/**
* 下载文件
* @param path
* @param fileName
*/
public void downloadFile(String dId) {
try {
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
.getResponse();
if(StringUtils.isNotEmpty(dId)){
ContentTransfer ct= getObjectStore().getFileDownDocument(dId);
downLoadFileForLocal(ct.accessContentStream(), ct.get_RetrievalName(),response);
}
} catch (Exception e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
}
/**
* 下载文件
* @param response
* @param fileName
* @param response
*/
public void downLoadFileForLocal(InputStream content, String fileName, HttpServletResponse response) {
try {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
fileName=fileName.replace("+"," ");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentType("application/x-msdownload;charset=UTF-8");
ServletOutputStream servletOutputStream = response.getOutputStream();
byte[] b = new byte[2048];
int i = 0;
while ((i = content.read(b)) > 0) {
servletOutputStream.write(b, 0, i);
}
servletOutputStream.flush();
servletOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
}