springMVC 다운로드 + 파일 이름 디코드 + 프로필에서 경로 읽기
1903 단어 springMVC
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);
}