如何使用Struts2上传Microsoft word .docx和.xlsx文件

时间:2012-06-12 14:23:12

标签: struts2

我们在应用程序中使用Struts2,并且我使用struts2上传功能进行文件上传。现在我的要求是,我们需要让用户上传到“.docx”和“.xlsx”。我在strut.xml中将“application / msword”和“application / vnd.ms-excel”列为allowedTypes。这样,我们就可以上传“。doc”和“.xls”文件,但不是“。docx”和“.xlsx”文件。有没有解决方法呢?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以通过验证处理上传文件的操作中的文件名来实现。例如:

private File attachement;

...
public void validate() {
  if (attachement.getName().endsWith(".docx") || (attachement.getName().endsWith(".xlsx")))
    addActionError(...);
}