2012-06-12 46 views
1

我們在我們的應用程序中使用Struts2,並且我正在使用struts2上傳功能來上傳文件。現在我的要求是,我們需要讓用戶上傳到「.docx」和「.xlsx」。我在strut.xml中以allowedTypes列出了「application/msword」和「application/vnd.ms-excel」。通過這個,我們能夠上傳只有「.doc」和「.xls」文件,但而不是「.docx」和「.xlsx」文件。有沒有解決方法?如何使用Struts2上傳Microsoft word .docx和.xlsx文件

+0

這是做你所說的。這些MIME類型對於docx或xlsx格式無效。請參閱nmc的答案。 –

回答

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 
0

你可以通過在處理上傳的文件的操作驗證的文件名做。例如:

private File attachement; 

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