Download and produce pdf files

The principle of servlet is applied here, and the type of response header is added to the jsp returned by the servlet: .pdf, and the browser recognition automatically changes to download

<servlet>
<servlet-name>downloadFile</servlet-name>
<servlet-class>com.wondersgroup .qdyth.p1.common.action.DownloadFileAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>downloadFile</servlet-name>
<url-pattern>/downloadFile</url-pattern>
</servlet-mapping>

//Assemble pdf map
DownloadFileService template file to generate

getDataSource//Assemble map

createpdf//Create pdf with map


ServletOutputStream out = response.getOutputStream();
       response.reset();
       response.setContentType("application/pdf ");
       response.setHeader("Content-Disposition", "attachment;filename=" +policy_no +".pdf");
       response.addHeader("Content-Length", "" + targetFile.length());
       File url = new File(targetFile_path);
       FileInputStream bis = new FileInputStream(url);
       BufferedOutputStream bos = new BufferedOutputStream(out);
       byte[] buff = new byte[2048];
       int bytesRead;
       while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
           bos.write(buff, 0, bytesRead);
       }
       response.setStatus( response.SC_OK );
       response.flushBuffer();
       bis.close();
       bos.close();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326940942&siteId=291194637