1

我们在我们的应用程序中使用 Struts2,我正在使用 struts2 上传功能进行文件上传。现在我的要求是,我们需要让用户上传到“.docx”和“.xlsx”。allowedTypes我在 strut.xml 中列出了“application/msword”和“application/vnd.ms-excel” 。这样,我们只能上传 .doc”和“.xls”文件,但不能上传“.docx”和“.xlsx”文件。有什么解决方法吗?

4

2 回答 2

3

来自http://sanjaal.com/java/tag/microsoft-office-2010-mime-types/

以下是用于 MS Office 2010 文档文件格式的 mime 类型。

.docm: application/vnd.ms-word.document.macroEnabled.12
.docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotm: application/vnd.ms-word.template.macroEnabled.12
.dotx: application/vnd.openxmlformats-officedocument.wordprocessingml.template
.potm: application/vnd.ms-powerpoint.template.macroEnabled.12
.potx: application/vnd.openxmlformats-officedocument.presentationml.template
.ppam: application/vnd.ms-powerpoint.addin.macroEnabled.12
.ppsm: application/vnd.ms-powerpoint.slideshow.macroEnabled.12
.ppsx: application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pptm: application/vnd.ms-powerpoint.presentation.macroEnabled.12
.pptm: application/vnd.ms-powerpoint.presentation.macroEnabled.12
.pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
.xlam: application/vnd.ms-excel.addin.macroEnabled.12
.xlsb: application/vnd.ms-excel.sheet.binary.macroEnabled.12
.xlsb: application/vnd.ms-excel.sheet.binary.macroEnabled.12
.xlsm: application/vnd.ms-excel.sheet.macroEnabled.12
.xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltm: application/vnd.ms-excel.template.macroEnabled.12
.xltx: application/vnd.openxmlformats-officedocument.spreadsheetml
.xps:  application/vnd.ms-xpsdocument
于 2012-06-12T14:26:14.517 回答
0

您可以通过在处理上传文件的操作中验证文件名来做到这一点。例如:

private File attachement;

...
public void validate() {
  if (attachement.getName().endsWith(".docx") || (attachement.getName().endsWith(".xlsx")))
    addActionError(...);
}
于 2012-06-12T15:33:29.283 回答