Skip to content

Instantly share code, notes, and snippets.

@keesun
Created January 13, 2012 03:19
Show Gist options
  • Save keesun/1604411 to your computer and use it in GitHub Desktop.
Save keesun/1604411 to your computer and use it in GitHub Desktop.
Servlet 3.0's FileUpload Sample
@WebServlet("/upload")
@MultipartConfig(location = "/tmp")
public class FileUploadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
getServletContext().getRequestDispatcher("/WEB-INF/views/fileUpload.jsp").forward(req, res);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Collection<Part> parts = req.getParts();
for(Part part : parts) {
System.out.println("Name:");
System.out.println(part.getName());
System.out.println("Header: ");
for(String headerName : part.getHeaderNames()) {
System.out.println(headerName);
System.out.println(part.getHeader(headerName));
}
System.out.println("Size: ");
System.out.println(part.getSize());
part.write(part.getName() + "-down");
}
res.sendRedirect("/upload");
}
}
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
@Controller
public class SpringFileUploadController{
@RequestMapping(value = "/s/upload", method = RequestMethod.GET)
public String fileUploadForm(){
return "/supload";
}
@RequestMapping(value = "/s/upload", method = RequestMethod.POST)
public String fileUploadSubmit(@RequestParam("file") Part part){
System.out.println(part.getName());
System.out.println(part.getHeader("content-disposition"));
System.out.println(part.getContentType());
System.out.println(part.getSize());
try {
part.write("sample");
} catch (IOException e) {
throw new RuntimeException(e);
}
return "redirect:/s/upload";
}
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>File Upload Sample</title>
</head>
<body>
<form action="/upload" enctype="multipart/form-data" method="post">
<p>
<label>Select a file: </label>
<input type="file" name="file"/>
</p>
<input type="submit" value="Upload" />
</form>
</body>
</html>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-servlet.xml</param-value>
</init-param>
<multipart-config>
<max-file-size>52428800</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
@mihn
Copy link

mihn commented Feb 7, 2014

It doesnt use Servlet 3.0 mutlipart handling, since you are using CommonsMultipartResolver :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment