81,122
社区成员




/**
* <p>
* 功能说明:将服务器上的文件下载到本地 文件名为原文件名
* </p>
*
* @param strfileName String 处理得到的文件URL+实际文件名
* @param S_Name String 原文件名
* @return void
* @throws IOException
*/
public static void downloadFileAsS_Name(String strfileName,String S_Name) throws IOException {
File exportFile = new File(strfileName);
try {
exportFile = FileEncrypt.dencrypt(exportFile); //解密
HttpServletResponse httpServletResponse = (HttpServletResponse)
FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
// 此处只写文件名exportFile.getName(),不需绝对路径
httpServletResponse.setHeader( "Content-Disposition",
"attachment;filename=" + new String(S_Name.getBytes("gb2312"),"ISO8859-1"));
httpServletResponse.setContentLength((int) exportFile.length());
httpServletResponse.setContentType("application/x-download");
byte[] b = new byte[1024];
int i = 0;
FileInputStream fis = new java.io.FileInputStream(exportFile);
while ((i = fis.read(b)) > 0) {
servletOutputStream.write(b, 0, i);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e);
throw e;
} catch (Exception e) {
e.printStackTrace();
}
exportFile.delete();
FacesContext.getCurrentInstance().responseComplete();
}