Created
January 13, 2012 03:19
-
-
Save keesun/1604411 to your computer and use it in GitHub Desktop.
Servlet 3.0's FileUpload Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It doesnt use Servlet 3.0 mutlipart handling, since you are using CommonsMultipartResolver :)