Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is step by step configuration for file uploading by using Apache Commons FileUpload:</p> <p><strong>1. Add dependency jars for the following component. Here is the maven dependencies:</strong><br/></p> <p>pom.xml</p> <pre><code> &lt;dependencies&gt; &lt;!-- Spring 3 MVC --&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;3.2.4.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Apache Commons file upload --&gt; &lt;dependency&gt; &lt;groupId&gt;commons-fileupload&lt;/groupId&gt; &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt; &lt;version&gt;1.2.2&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Apache Commons IO --&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.commons&lt;/groupId&gt; &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;1.3.2&lt;/version&gt; &lt;/dependency&gt; &lt;!-- JSTL for c: tag --&gt; &lt;dependency&gt; &lt;groupId&gt;jstl&lt;/groupId&gt; &lt;artifactId&gt;jstl&lt;/artifactId&gt; &lt;version&gt;1.2&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; </code></pre> <p>If you are not using maven then download respective jar from the maven repository online.</p> <p><strong>2. Create a FileUploadForm model</strong><br/></p> <p>FileUploadForm.java</p> <pre><code>import java.util.List; import org.springframework.web.multipart.MultipartFile; public class FileUploadForm { private List&lt;MultipartFile&gt; files; //Getter and setter methods } </code></pre> <p><strong>3. Add resolver to MVC config file</strong></p> <pre><code>&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt; &lt;!-- one of the properties available; the maximum file size in bytes --&gt; &lt;property name="maxUploadSize" value="100000"/&gt; &lt;/bean&gt; </code></pre> <p><strong>4. Write FileUploadController</strong><br/></p> <p>FileUploadController.java</p> <pre><code>@Controller public class FileUploadController {           @RequestMapping(value = "/show", method = RequestMethod.GET)     public String displayForm() {         return "file_upload_form";     }           @RequestMapping(value = "/save", method = RequestMethod.POST)     public String save(             @ModelAttribute("uploadForm") FileUploadForm uploadForm,                     Model map) {                   List&lt;MultipartFile&gt; files = uploadForm.getFiles();           List&lt;String&gt; fileNames = new ArrayList&lt;String&gt;();                   if(null != files &amp;&amp; files.size() &gt; 0) {             for (MultipartFile multipartFile : files) {                   String fileName = multipartFile.getOriginalFilename();                 fileNames.add(fileName);                 //Handle file content - multipartFile.getInputStream()               }         }                   map.addAttribute("files", fileNames);         return "file_upload_success";     } } </code></pre> <p><strong>5. Write jsp views <br/></strong></p> <p>file_upload_form.jsp</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Spring MVC Multiple File Upload&lt;/title&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function() { //add more file components if Add is clicked $('#addFile').click(function() { var fileIndex = $('#fileTable tr').children().length - 1; $('#fileTable').append( '&lt;tr&gt;&lt;td&gt;'+ ' &lt;input type="file" name="files['+ fileIndex +']" /&gt;'+ '&lt;/td&gt;&lt;/tr&gt;'); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Spring Multiple File Upload example&lt;/h1&gt; &lt;form method="post" action="save.html" **enctype="multipart/form-data"**&gt; &lt;p&gt;Select files to upload. Press Add button to add more file inputs.&lt;/p&gt; &lt;input id="addFile" type="button" value="Add File" /&gt; &lt;table id="fileTable"&gt; &lt;tr&gt; &lt;td&gt;&lt;input name="files[0]" type="file" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input name="files[1]" type="file" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br/&gt;&lt;input type="submit" value="Upload" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Reference: <a href="http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart" rel="nofollow">http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload