public ModelAndView download(HttpServletRequest request,HttpServletResponse response) throws Exception {
String fileName = request.getParameter("fileName");
String filePath = readProperties("report.path.prefix")+"/supplier/"+fileName;
File file = new File(filePath);
// 清空response
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gbk"),"iso-8859-1") + "\"");
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream;charset=UTF-8");
InputStream in = null;
OutputStream toClient = null;
try{
//以流的形式下载文件
in = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
toClient.close();
}
return null;
}
public static Properties proper= null;
private String readProperties(String key){
if(this.proper==null){
InputStream in = this.getClass().getResourceAsStream("/env.properties");
try {
this.proper = new Properties();
this.proper.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
return this.proper.getProperty(key);
}
springMVC下载+文件名乱码+从配置文件读取路径
最新推荐文章于 2024-07-04 11:22:21 发布