I am trying to implement the file upload process in my web application using struts2 fileUpload interceptor. below is my code in
index.jsp
<tags:form action="fileUpload" method="post" enctype="multipart/form-data">
<tags:file name="fileUpload" label="Choose File"/>
<tags:submit value="Upload"/>
</tags:form>
struts.xml
<action name="fileUpload" class="com.hibernate.action.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
<result name="success">/viewChapters.jsp</result>
</action>
FileUploadAction.java
public class FileUploadAction extends ActionSupport
{
private File fileUpload;
private String contentType;
private String fileName;
private String destPath;
/// setter and getter methods
public String execute()
{
destPath="C:\\WebPortal_testing";
try
{
System.out.println("Source File Name:"+fileUpload);
System.out.println("Destination File Name:"+fileName);
File destFile= new File(destPath,fileName);
FileUtils.copyFile(fileUpload, destFile);
}
catch(IOException exception)
{
exception.printStackTrace();
return ERROR;
}
return SUCCESS;
}
when I select a pdf file in the index.jsp page and click on upload button it is giving null value to the fileUpload field of the action class.
I am executing the application in debug mode and gave this
System.out.println("Source File Name:"+fileUpload);
to check what it is returning and I am getting null.
1. Interceptor configuration is wrong
FileUploadStack is:
<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>
then what you're really defining is:
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
Using
- two times the fileUpload interceptor
- applying your limitations on maximumSize and allowedTypes only to the second.
Just do
<interceptor-ref name="fileUploadStack">
<param name="fileUpload.maximumSize">1024000</param>
<param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref>
2. File attributes are wrong
Content type and file name attributes must start with the File attribute name.
In your case:
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
You can find a full example on this question.
3. You are printing the File instead of the filename
System.out.println("Source File Name:"+fileUpload);
That is the file, not the filename, and btw the filename is passed in the other variable.
Fix this and retry. Also note that is not safe to use <tags:
as prefix when the whole world is using <s:
. There's no gain in doing that, only complications. Just use <s:
.
来源:https://stackoverflow.com/questions/33696901/struts2-fileupload-giving-null-file-in-the-action-class