我正在 Struts2 应用程序中执行文件上传过程。当我在动作类中显示文件名和内容类型时,它显示空指针异常。
我已经包含了 jar 文件并使用了以下代码。
在上传.jsp
<s:form action="saveBulkStores.action" method="POST" enctype="multipart/form-data">
<s:actionmessage name="message"/>
<s:label value="File Name : *" />
<s:file name="upload" label="File" size="40"/>
<br>
<br>
<s:submit name="ADD" value="UPLOAD"/>
<input type="button" onClick="cancelFunction()" name="Cancel" value="Cancel" tabindex="10">
</s:form>
在 struts.xml 中
<action name="saveBulkStores" class="com.rewardz.action.FilesUploadAction" method="saveBulkStores">
<interceptor-ref name="fileUpload">
<param name="maximumSize">52428800</param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<result name="input">/uploadfile.jsp</result>
<result name="viewFile">/viewfile.jsp</result>
<result name="Reload">/uploadfile.jsp</result>
</action>
在动作类中:
private File file;
private String contentType;
private String filename;
public void setUpload(File file) {
this.file = file;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public void setUploadFileName(String filename) {
this.filename = filename;
}
public String saveBulkStores() throws IOException{
System.out.println("check Bulk upload file");
String filePath = request.getRealPath("/");
System.out.println("Server path:" + filePath);
System.out.println("UPLOADFILECONTENT TYPE:"+contentType);
System.out.println("UPLOADFILENAME:"+filename);
System.out.println("UPLOADFILE:"+file);
System.out.println("**********************************");
value = "viewFile";
System.out.println("Forward Value:"+value);
return value;
}
Server.log 中的输出:(使用 GlassFish Server)
check Bulk upload file
C:\glassfish3_installer\glassfish\domains\domain1\applications\My_Application\
UPLOADFILECONTENT TYPE:null
UPLOADFILENAME:null
UPLOADFILE:null
所以请任何人帮助我获取Struts2上传过程中的文件名,文件路径。