iText(最新版本)似乎工作得稍微好一些,因为非 Acrobat pdf 查看器可以看到它。使用 PDFBox 浏览和预览仅显示空白页,不提示通过。也就是说,当 Content-Disposition = inline 显示乱码时,我在加密现有文件 - Android 后遇到了 iText 问题。我仍在测试 CD = 附件。
我想要在 iText 或 PDFBox 中从文件系统/db(future)中获取文件,对其进行加密,然后通过 Web 应用程序服务器(jBoss 7.1.1.Final)将其发送回。然后,浏览器应确定是否内联查看器(如果它有 pdf 查看器)或下载它以用于其他应用程序。顺便说一句,这在 iOS 中运行良好。下面是我的 iText 和 PDFBox 测试代码。
@RequestMapping (value = "/pdf/{page}.pdf", method = RequestMethod.GET)
public void doDownload(@PathVariable("page") String paramString,
HttpServletRequest request, HttpServletResponse response) throws IOException {
// get absolute path of the application
ServletContext context = request.getServletContext();
String appPath = context.getRealPath("");
System.out.println("appPath = " + appPath);
// construct the complete absolute path of the file
String fullPath = appPath + "/pdf/" + paramString + ".pdf";
File downloadFile = new File(fullPath);
PDDocument mypdf = PDDocument.load(downloadFile);
// try {
// mypdf.encrypt("demo", "demo");
// } catch (CryptographyException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// FileInputStream inputStream = new FileInputStream(downloadFile);
// get MIME type of the file
String mimeType = context.getMimeType(fullPath);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/pdf";
}
System.out.println("MIME type: " + mimeType);
// set content attributes for the response
// response.setContentType(mimeType);
// response.setContentLength((int) downloadFile.length());
//
// set headers for the response
response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
// response.addHeader("Content-Transfer-Encoding", "binary");
// get output stream of the response
OutputStream outStream = response.getOutputStream();
try {
mypdf.save(outStream);
mypdf.close();
} catch (COSVisitorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// byte[] buffer = new byte[BUFFER_SIZE];
// int bytesRead = -1;
//
// // write bytes read from the input stream into the output stream
// while ((bytesRead = inputStream.read(buffer)) != -1) {
// outStream.write(buffer, 0, bytesRead);
// }
// inputStream.close();
// outStream.flush();
// outStream.close();
// return "pages-authenticated/pdf";
}
//}
// PDFBox
@RequestMapping (value = "pages-authenticated/pdf.html", method = RequestMethod.POST)
public String doDownload(Model model, @ModelAttribute("PDFForm") PDFForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
// get absolute path of the application
ServletContext context = request.getServletContext();
String appPath = context.getRealPath("");
String fullPath;
System.out.println("appPath = " + appPath);
System.out.println("pass:" + form.getPass());
// Return if no password given
if (form.getPass() == null || form.getPass().equalsIgnoreCase("")) {
model.addAttribute("noPass", true);
return "pages-authenticated/pdf";
}
// construct the complete absolute path of the file
fullPath = appPath + "/pdf/notice.pdf";
File downloadFile = new File(fullPath);
PDDocument mypdf = PDDocument.load(downloadFile);
try {
mypdf.encrypt(form.getPass(),form.getPass());
} catch (CryptographyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// FileInputStream inputStream = new FileInputStream(downloadFile);
// get MIME type of the file
String mimeType = context.getMimeType(fullPath);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/pdf";
}
System.out.println("MIME type: " + mimeType);
// set headers for the response
response.setContentType("application/pdf");
// response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
// get output stream of the response
OutputStream outStream = response.getOutputStream();
try {
mypdf.save(outStream);
outStream.flush();
outStream.close();
mypdf.close();
} catch (COSVisitorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// iText
//@RequestMapping (value = "pages-authenticated/pdf.html", method = RequestMethod.POST)
//public String doDownload(Model model, @ModelAttribute("PDFForm") PDFForm form,
// HttpServletRequest request, HttpServletResponse response) throws IOException {
// // get absolute path of the application
// ServletContext context = request.getServletContext();
// String appPath = context.getRealPath("");
// String fullPath;
// System.out.println("appPath = " + appPath);
//
//
// System.out.println("pass:" + form.getPass());
//
//// // set headers for the response
// response.setContentType("application/pdf");
// response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
//
//
// // Return if no password given
// if (form.getPass() == null || form.getPass().equalsIgnoreCase("")) {
// model.addAttribute("noPass", true);
////
//// fullPath = appPath + "/WEB-INF/pages-authenticated/pdf.html";
//// File downloadFile = new File(fullPath);
////
//// FileInputStream inputStream = new FileInputStream(downloadFile);
////
////// set content attributes for the response
//// response.setContentType("text/html");
//// response.setContentLength((int) downloadFile.length());
////
//// byte[] buffer = new byte[BUFFER_SIZE];
//// int bytesRead = -1;
////
//// // write bytes read from the input stream into the output stream
//// while ((bytesRead = inputStream.read(buffer)) != -1) {
//// outStream.write(buffer, 0, bytesRead);
//// }
////
////
//// inputStream.close();
//// outStream.flush();
//// outStream.close();
//
// return "pages-authenticated/pdf";
//
// }
// // construct the complete absolute path of the file
// fullPath = appPath + "/pdf/notice.pdf";
//
// PdfReader mypdf = new PdfReader(fullPath);
// PdfStamper myPdfStamper;
// try {
// myPdfStamper = new PdfStamper(mypdf, response.getOutputStream());
// myPdfStamper.setEncryption(form.getPass().getBytes(), form.getPass().getBytes(),
// PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_256);
// myPdfStamper.close();
// mypdf.close();
// } catch (DocumentException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
//
//
//// FileInputStream inputStream = new FileInputStream(downloadFile);
//
// // get MIME type of the file
//// String mimeType = context.getMimeType(fullPath);
//// if (mimeType == null) {
//// // set to binary type if MIME mapping not found
//// mimeType = "application/pdf";
//// }
//// System.out.println("MIME type: " + mimeType);
////
//// // set headers for the response
//// response.setContentType("application/pdf");
//// response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
////
//// // get output stream of the response
//// OutputStream outStream = response.getOutputStream();
////
//// try {
//// mypdf.save(outStream);
//// outStream.flush();
//// mypdf.close();
////
//// } catch (COSVisitorException e) {
//// // TODO Auto-generated catch block
//// e.printStackTrace();
//// }
////
// return null;
// }
}