iText(最新版本)看起来效果稍好,因为非Acrobat pdf观众可以看到它。脱脂&使用PDFBox预览仅显示空白页面,无需提示传递。也就是说,在加密现有文件(Android)时,我遇到了iText的问题,当Content-Disposition = inline显示乱码时。我还在测试CD =附件。
iText或PDFBox中我想要的是从文件系统/ db(将来)获取文件,对其进行加密,然后通过Web应用服务器(jBoss 7.1.1.Final)将其发回。然后,浏览器应确定是否内联查看器是否具有pdf查看器或将其下载到另一个应用程序。这适用于iOS,顺便说一下。以下是我的iText和amp;的代码。 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;
// }
}
答案 0 :(得分:0)
这是我最终的工作(Android问题):
1.不能使用AES 256,但AES 128可以使用
2. Cannot use POST, must use GET