页面生成json文件并下载

下载文件

 

function getDescription(){
      var url=_urlPath+"/downloadDescription";
       var form=$("
"); form.attr("style","display:none"); form.attr("target",""); form.attr("method","post"); form.attr("action",url); $("body").append(form); form.submit(); }

 

@RequestMapping("/downloadDescription")
public void download(HttpServletResponse response) {
    List listUser = userService.getAllUser();
    String jsonArray = JSON.toJSONString(listUser);
    // 导出文件
    response.setContentType("text/plain");
    String fileName = "文件";
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".json");
    BufferedOutputStream buff = null;
    StringBuffer write = new StringBuffer();
    ServletOutputStream outSTr = null;
    try {
        outSTr = response.getOutputStream(); // 建立
        buff = new BufferedOutputStream(outSTr);
        write.append(jsonArray);
        buff.write(write.toString().getBytes("UTF-8"));
        buff.flush();
        buff.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            buff.close();
            outSTr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(JavaWeb,Java总结的问题,Web前端)