@RequestMapping(value="/download")
public @ResponseBody String download(HttpServletResponse response,HttpServletRequest request,@Param(value="filePath") String filePath,@Param(value="")String id ) {
try {
NjWorkLog njWorkLog= njWorkLogService.findOneById(id);
// path是指欲下载的文件的路径。
File file = new File(request.getRealPath(filePath));
// 取得文件名。
//String filename = file.getName();
// 取得文件的后缀名。
// String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
String filename=njWorkLog.getStrFileName();
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
filename = URLEncoder.encode(filename, "UTF-8");//IE浏览器
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
response.addHeader("content-type","application/octet-stream");
response.addHeader("Content-Disposition","attachment;filename="+filename);
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public @ResponseBody String download(HttpServletResponse response,HttpServletRequest request,@Param(value="filePath") String filePath,@Param(value="")String id ) {
try {
NjWorkLog njWorkLog= njWorkLogService.findOneById(id);
// path是指欲下载的文件的路径。
File file = new File(request.getRealPath(filePath));
// 取得文件名。
//String filename = file.getName();
// 取得文件的后缀名。
// String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
String filename=njWorkLog.getStrFileName();
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
filename = URLEncoder.encode(filename, "UTF-8");//IE浏览器
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
response.addHeader("content-type","application/octet-stream");
response.addHeader("Content-Disposition","attachment;filename="+filename);
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}