public void export(String resId){ // path是指欲下载的文件的路径。
String path = ((HttpServletRequest)Result.getServletRequest()).getRealPath("/resume")+"\\Resume_"+resId+".doc";
OutputStream toClient = null;
try {
File file = new File(path);
// 取得文件名。
String name = file.getName();
String ext = name.substring(name.lastIndexOf(".") + 1).toUpperCase();
// 取得文件的后缀名。
// 以流的形式下载文件。
InputStream fis;
fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
HttpServletResponse response =((HttpServletResponse)Result.getServletResponse());
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + name);
response.addHeader("Content-Length", "" + file.length());
toClient = response.getOutputStream();
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(toClient != null){
toClient.flush();
toClient.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
文件下载代码
最新推荐文章于 2021-04-14 17:59:40 发布