public void getIdentificationBarFile(String formdataid, HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> searchParam = new HashMap<String, Object>();
searchParam.put("form_data_id", formdataid);
String pdfPath = null;
try {
pdfPath = taskinformationservice.getIdentificationBarFile(searchParam);
} catch (Exception e1) {
e1.printStackTrace();
}
if (pdfPath != null) {
File pdf = new File(pdfPath);
response.setContentType("text/html;charset=" + "UTF-8");
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfPath));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());) {
//必写,流必须关闭,java7中关闭流可以写在try里,就不用手动finally里close了
response.setContentType("application/pdf");// 不同类型的文件对应不同的MIME类型 // 重点突出
response.setHeader("Content-disposition",
"attachment; filename=" + new String("标识条.pdf".getBytes("UTF-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(pdf.length()));
//bis.available()返回缓冲区中要读取的剩余字节数的总和
byte[] buffer = new byte[bis.available()];
bis.read(buffer);
bos.write(buffer);
} catch (Exception e) {
e.printStackTrace();
}
}
/*response.setContentType("text/html;charset=" + "UTF-8");
try {
Map<String, Object> searchParam = new HashMap<String, Object>();
searchParam.put("form_data_id", formdataid);
String pdfPath = taskinformationservice.getIdentificationBarFile(searchParam);
if(pdfPath!=null){
File pdf = new File(pdfPath);
response.setContentType("application/x-msdownload;");// 不同类型的文件对应不同的MIME类型 // 重点突出
response.setHeader("Content-disposition","attachment; filename=" + new String("标识条.pdf".getBytes("UTF-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(pdf.length()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfPath));//这里不是文件pdf,而是文件地址
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2048];
int bytesRead;//获得buffer的长度,没有值返回-1
while (-1 != (bytesRead = bis.read(buffer, 0, buffer.length))) {
bos.write(buffer, 0, bytesRead);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//必写,流必须关闭,java7中关闭流可以写在try里,就不用手动finally里close了
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
response下载文件pdf
最新推荐文章于 2024-04-25 16:26:40 发布