SpringBoot XML文件下载

 public void taskDownload(HttpServletResponse response) {
   try {
    	File file=new File("E:\\Temp\\ED-15D20190507G5051-FuDan01.XML");
    	 if(file.exists()){
          InputStream is=new FileInputStream(file);
    	 OutputStream os=response.getOutputStream();
     	BufferedInputStream bis = new BufferedInputStream(is);
     	BufferedOutputStream bos = new BufferedOutputStream(os);
     	String fileName="ED-15D20190507G5051-FuDan01.XML";
     	fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
     	fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
     	response.reset();
     	response.setCharacterEncoding("UTF-8");
     	response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
     	response.setHeader("Content-Disposition", "attachment; filename="+fileName);
     	int bytesRead = 0;
     	byte[] buffer = new byte[1024];
     	while ((bytesRead = bis.read(buffer)) != -1){
     	bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
   	  }
   	bos.flush();
   	bis.close();
   	bos.close();
   	is.close();
   	os.close();
   	}
   	} catch (Exception e) {
   	// TODO: handle exception
   	}
   }

你可能感兴趣的:(SpringBoot XML文件下载)