java
File file = new File(path);
String fileName = file.getName();
// 这三行 弹出下载路径选择框
response.setContentType("application/octet-stream");
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//**********************************
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
try {
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
PDFUtil.setFileDownloadHeader(request, response, fileName);
int byteRead = 0;
byte[] buffer = new byte[8192];
while ((byteRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, byteRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
}
setFileDownloadHeader 方法
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
String fileName) {
try {
// 中文文件名支持
String encodedfileName = null;
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) {// IE
encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {
encodedfileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
} else {
encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
html
function creditReport() {
window.location.href = '${pageContext.request.contextPath}/pdf/createPDF.do?bondcode='
+ bondCode;