Skip to content

Instantly share code, notes, and snippets.

@fangj
Forked from keesun/FileUploadServlet.java
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fangj/0bb47d301ffa8aea6f56 to your computer and use it in GitHub Desktop.
Save fangj/0bb47d301ffa8aea6f56 to your computer and use it in GitHub Desktop.
@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment