上传及下载

导入脚本接口
 * @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);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值