导入脚本接口 * @param request */ @RequestMapping(value = "/importRule.json") @ResponseBody public void importRule(HttpServletRequest request) { InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; StringBuffer stringBuffer = new StringBuffer(""); //将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); //检查form中是否有enctype="multipart/form-data" if(multipartResolver.isMultipart(request)) { //将request变成多部分request MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request; //获取multiRequest 中所有的文件名 Iterator iter=multiRequest.getFileNames(); while(iter.hasNext()) { //一次遍历所有文件 MultipartFile file=multiRequest.getFile(iter.next().toString()); if(file!=null) { try { inputStream = file.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); String content = ""; while ((content = bufferedReader.readLine()) != null) { stringBuffer.append(content); // 一次读入一行数据 } } catch (IOException e) { e.printStackTrace(); } } } } List<RuleForm> ruleFormList = JSON.parseArray(new String(stringBuffer),RuleForm.class); }
public static void exportJsonToTxt(HttpServletResponse response, Object result, Logger logger) { String name = null; PrintWriter printWriter = null; try { name = URLEncoder.encode("脚本", "UTF-8"); response.setHeader("Content-Disposition", "attachment;filename="+name); response.setContentType("application/force-download");//应用程序强制下载 response.setCharacterEncoding("UTF-8"); printWriter = response.getWriter(); String content = JSONObject.toJSONString(result, nameFilter, SerializerFeature.DisableCircularReferenceDetect); printWriter.println(content); } catch (Exception e) { logger.error("printWriter输出json失败!", e); } finally { if (printWriter != null) { try { printWriter.close(); } catch (Exception e) { logger.error("out.close()出现异常", e); } } }