0

I am facing an issue with multipart file upload functionality in my spring application. I am using struts2.3 tags in few other jsp files in my application. Upload functionlity was working fine before I migrated struts from 1.2 to 2.3 in my spring app.

code snippet in my upload jsp file:

<td nowrap="nowrap">
                <input type="file" name="file" class="formElement" size="40"/>&nbsp;
                <input name="actionAdd" type="button" class="button" value="Add File to Table" onclick="addFileToTable();"/>
                <div id="noFileSelected" style="display:none"><p><span class="error">No File selected.</span> </p></div>
            </td>

code block in Implementation class:

multiPartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multiPartRequest.getFile("file");

code in spring config file

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="500000"/>
</bean>

multiPartRequest.getFile("file") returning null.

the same code block was working fine before.

2 Answers2

0

Try getting direclty from file object do not use multipart request to get the file.

private List<File> file; // if you want to select multiple files
private String fileFileName;
private String fileContentType;

    for (File file : file){

        System.out.print("\nFile ["+i+"] ");
                      System.out.print("title:"+imgTitle);
                      System.out.print("; name:"         + fileFileName.get(i));
                      System.out.print("; contentType: " + fileContentType.get(i));
                      System.out.print("; length: "      + file.length());
    }
nansjames
  • 35
  • 1
  • 8
  • If you use a List for File object, you should use a List for the fileNames and contentTypes too. Also, since your code is identical to [the one in this question](http://stackoverflow.com/a/17212916/1654265), you could have provided a reference to the original post. Finally, it is not clear what imgTitle is, since it's not defined anywhere – Andrea Ligios Dec 01 '14 at 17:43
0

I guess you have Struts2 with Spring-plugin + Spring libraries (and not Spring MVC, that is an alternative to Struts2); then why using Spring to upload files, when Struts2 is even better, and with no code to write, in doing it ?

JSP

<input type="file" name="file" class="formElement" size="40"/>

Action

private File file;
private String fileFileName;
private String fileContentType;

/* Getters and Setters */

Ensure to have the FileUpload Interceptor in your stack (if you haven't touched anything, you have it), that's it. Fully automatic.

It also works perfectly with multiple files.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243