【发布时间】:2014-06-03 18:32:15
【问题描述】:
我有一个允许用户上传附件的网络应用程序;但是,我想将用户限制为仅限某些文件类型 - Adobe PDF 和 MS Excel。 原因是在用户提交文档进行处理和工作流之前,我将汇总一些附件并创建一个 PDF 报告。
我做了一些研究,转换 DOC(X)、RTF 等......会很头疼。 另外,如果附件都是 PDF 格式,每个人“理论上”都会获得更好的查看便携性。
目前我正在检查 mime 类型 -
PDF - "application/pdf"
XLS(X) -
"application/vnd.ms-excel""application/msexcel""application/x-msexcel""application/x-ms-excel""application/x-excel""application/x-dos_ms_excel""application/xls""application/x-xls"
这很好用,但我注意到我可以使用 .docx 文件并将其扩展名更改为 .pdf 并成功绕过此检查。
为了解决这个问题,我计划进一步检查实际文件的标题。
根据this library的文件签名
PDF 将具有以下标题 -
25 50 44 46
并且它将有以下预告片之一 -
0A 25 25 45 4F 46 (.%%EOF)0A 25 25 45 4F 46 0A (.%%EOF.)0D 0A 25 25 45 4F 46 0D 0A (..%%EOF..)0D 25 25 45 4F 46 0D (.%%EOF.)
到目前为止,我有执行此检查的框架代码 -
** 已编辑以反映答案 **
public boolean confirmAttachmentAuthenticity(ProposalDevelopmentForm form, String mimeType) {
boolean authentic = true;
// Case: User is attempting to upload a "PDF" document
if (mimeType.equals(ADOBE_PDF_CONTENT_TYPE)) {
try {
InputStream inputStream = form.getNewNarrative().getNarrativeFile().getInputStream();
PdfReader pdfReader = new PdfReader(inputStream);
int numberOfPages = pdfReader.getNumberOfPages();
if (numberOfPages > 0) {
// Success - valid PDF
info(form.getNewNarrative().getNarrativeFile().getFileName() + " validated authentic Adobe PDF file");
}
}
catch(IOException ioe) {
// Failure - masquerading PDF
authentic = false;
info(form.getNewNarrative().getNarrativeFile().getFileName() + " is not an authentic Adobe PDF file.");
reportError("newNarrative.narrativeFile",
KeyConstants.ERROR_ATTACHMENT_PDF_NOT_AUTHENTIC,
form.getNewNarrative().getNarrativeFile().getFileName());
}
catch (Exception e) {
// Failure - other causes
authentic = false;
info(form.getNewNarrative().getNarrativeFile().getFileName() + " could not be authenticated at this time.");
e.printStackTrace();
reportError("newNarrative.narrativeFile",
KeyConstants.ERROR_ATTACHMENT_TYPE_CORRUPTED,
form.getNewNarrative().getNarrativeFile().getFileName());
}
}
// Case: User is attempting to upload an "EXCEL" spreadsheet
else {
try {
InputStream inputStream = form.getNewNarrative().getNarrativeFile().getInputStream();
POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
int numberOfSheets = workBook.getNumberOfSheets();
if (numberOfSheets > 0) {
// Success - valid Excel Spreadsheet
info(form.getNewNarrative().getNarrativeFile().getFileName() + " validated authentic MS Excel file");
}
}
catch(IOException ioe) {
// Failure - masquerading XLS(X)
authentic = false;
info(form.getNewNarrative().getNarrativeFile().getFileName() + " is not an authentic MS Excel file.");
reportError("newNarrative.narrativeFile",
KeyConstants.ERROR_ATTACHMENT_XLS_NOT_AUTHENTIC,
form.getNewNarrative().getNarrativeFile().getFileName());
}
catch (Exception e) {
// Failure - other causes
authentic = false;
info(form.getNewNarrative().getNarrativeFile().getFileName() + " could not be authenticated at this time.");
e.printStackTrace();
reportError("newNarrative.narrativeFile",
KeyConstants.ERROR_ATTACHMENT_TYPE_CORRUPTED,
form.getNewNarrative().getNarrativeFile().getFileName());
}
}
return authentic;
}
我认为最好的方法是使用BinarySearch 方法来执行此操作。
但是,我也读过一些帖子,人们建议将 fileData 转换为字符串,然后使用正则表达式。
任何想法都将不胜感激。
如果您能帮助我开始为任何一种情况填写我的骨架代码,则可以获得奖励积分。 我的位逻辑知识已经生疏了。这就是我在过去一年中编写的主要是高级客户端代码的结果。
【问题讨论】:
标签: java excel pdf pattern-matching bytearray