Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I finally figured out a solution that works using a HandlerExceptionResolver.</p> <p><strong>Add multipart resolver to your Spring config</strong>:</p> <pre><code>&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt; &lt;!-- the maximum size of an uploaded file in bytes --&gt; &lt;!-- &lt;property name="maxUploadSize" value="10000000"/&gt; --&gt; &lt;property name="maxUploadSize" value="1000"/&gt; &lt;/bean&gt; </code></pre> <p><strong>Model - UploadedFile.java</strong>:</p> <pre><code>package com.mypkg.models; import org.springframework.web.multipart.commons.CommonsMultipartFile; public class UploadedFile { private String title; private CommonsMultipartFile fileData; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public CommonsMultipartFile getFileData() { return fileData; } public void setFileData(CommonsMultipartFile fileData) { this.fileData = fileData; } } </code></pre> <p><strong>View - /upload.jsp</strong>:</p> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%&gt; &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%&gt; &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Test File Upload&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Select a file to upload&lt;/h1&gt; &lt;c:if test="${not empty errors}"&gt; &lt;h2 style="color:red;"&gt;${errors}.&lt;/h2&gt; &lt;/c:if&gt; &lt;form:form modelAttribute="uploadedFile" method="post" enctype="multipart/form-data" name="uploadedFileform" id="uploadedFileform"&gt; &lt;table width="600" border="0" align="left" cellpadding="0" cellspacing="0" id="pdf_upload_form"&gt; &lt;tr&gt; &lt;td width="180"&gt;&lt;label class="title"&gt;Title:&lt;/label&gt;&lt;/td&gt; &lt;td width="420"&gt;&lt;form:input id="title" path="title" cssClass="areaInput" size="30" maxlength="128"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="180"&gt;&lt;label class="title"&gt;File:&lt;/label&gt;&lt;/td&gt; &lt;td width="420"&gt;&lt;form:input id="fileData" path="fileData" type="file" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="180"&gt;&lt;/td&gt; &lt;td width="420"&gt;&lt;input type="submit" value="Upload File" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Controller - FileUploadController.java</strong>: package com.mypkg.controllers;</p> <pre><code>import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import com.mypkg.models.UploadedFile; @Controller public class FileUploadController implements HandlerExceptionResolver { @RequestMapping(value = "/upload", method = RequestMethod.GET) public String getUploadForm(Model model) { model.addAttribute("uploadedFile", new UploadedFile()); return "/upload"; } @RequestMapping(value = "/upload", method = RequestMethod.POST) public String create(UploadedFile uploadedFile, BindingResult result) { // Do something with the file System.out.println("######### File Uploaded with Title: " + uploadedFile.getTitle()); System.out.println("######### Creating local file: /var/test-file-upload/" + uploadedFile.getFileData().getOriginalFilename()); try { InputStream in = uploadedFile.getFileData().getInputStream(); FileOutputStream f = new FileOutputStream( "/var/test-file-upload/" + uploadedFile.getFileData().getOriginalFilename()); int ch = 0; while ((ch = in.read()) != -1) { f.write(ch); } f.flush(); f.close(); } catch (IOException e) { e.printStackTrace(); } return "redirect:/"; } /*** Trap Exceptions during the upload and show errors back in view form ***/ public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;(); if (exception instanceof MaxUploadSizeExceededException) { model.put("errors", exception.getMessage()); } else { model.put("errors", "Unexpected error: " + exception.getMessage()); } model.put("uploadedFile", new UploadedFile()); return new ModelAndView("/upload", model); } } ======================================================================== </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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