下载文件示例

js 中点击按钮,触发后台代码
function createReport(fileName){
    location.href=IPLATUI.CONTEXT_PATH+"/ireport/downloadLocal?fileName="+fileName;
}

===========================================================================================

后端代码

@RequestMapping(value="/downloadLocal", method = RequestMethod.GET)
public void downloadLocal(String fileName, HttpServletResponse response) throws IOException {
   InputStream inStream = new FileInputStream(new File(文件路径));// 文件的存放路径
   OutputStream out = response.getOutputStream();

   response.reset();
   response.setContentType("application/x-download");
   response.setHeader(
         "Content-disposition",
         "attachment;filename="
               + URLEncoder.encode(fileName, "UTF-8"));
   byte[] b = new byte[100];
   int len;
   try {
      while ((len = inStream.read(b)) > 0)
         out.write(b);
      inStream.close();
   } catch (IOException e) {
      e.printStackTrace();
   }finally {
      out.flush();
      out.close();
   }
}

猜你喜欢

转载自blog.csdn.net/u013764330/article/details/82995895