Multiparthttpservletrequest implementation of file uploads in spring
Posted from: http://my.oschina.net/nyniuch/blog/185266
Implement image upload
Users must be able to upload images, so the ability to upload files is required. The more common file upload components are commons FileUpload (http://jakarta.apache.org/commons/fileupload/a>) and Cos FileUpload (HTTP/ Www.servlets.com/cos), Spring has fully integrated both of these components, and here we choose commons FileUpload.
Because post a form that contains file uploads is sent to the server as a multipart/form-data request, you must explicitly tell Dispatcherservlet how to handle the multipartrequest. First declare a multipartresolver in Dispatcher-servlet.xml:
XML code
- <bean id= "Multipartresolver"
- class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
- <!--set the maximum size of the upload file to 1MB
- <property name= "Maxuploadsize" >
- <value>1048576</value>
- </property>
- </bean>
So once a request is a multipartrequest, it is first processed by Multipartresolver and then forwarded to the appropriate controller.
In Uploadimagecontroller, HttpServletRequest is transformed into Multiparthttpservletrequest, which makes it easy to get file names and file contents:
Java code
- Public Modelandview HandleRequest (HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- Transition to Multiparthttprequest:
- Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
- Get the file:
- Multipartfile file = multipartrequest.getfile ("file");
- Get file name:
- String filename = File.getoriginalfilename ();
- Get input stream:
- InputStream input = File.getinputstream ();
- Write file
- Or:
- File Source = new file (localfilename.tostring ());
- Multipartfile.transferto (source);
- }
Generate thumbnails (Table of contents)
When a user uploads a picture, a thumbnail must be generated so that the user can quickly navigate. We don't need third-party software, and the JDK standard library contains an API for image processing. We scaled a picture proportionally to the 120x120 size, and here's the key code:
Java code
- Public Static void createpreviewimage (string srcfile, String destfile) {
- Try {
- File fi = new file (srcfile); Src
- File fo = new file (destfile); Dest
- BufferedImage bis = imageio.read (FI);
- int w = bis.getwidth ();
- int h = bis.getheight ();
- Double scale = (double) w/h;
- int NW = image_size; Final int image_size = 120;
- int nh = (NW * h)/w;
- if (NH > Image_size) {
- NH = image_size;
- NW = (NH * w)/h;
- }
- Double sx = (double) nw/w;
- double sy = (double) nh/h;
- Transform.settoscale (SX, SY);
- Affinetransformop ato = new affinetransformop (transform, null);
- BufferedImage bid = new bufferedimage (NW, NH,
- BUFFEREDIMAGE.TYPE_3BYTE_BGR);
- Ato.filter (bis, Bid);
- Imageio.write (BID, "jpeg", FO);
- } catch (Exception e) {
- E.printstacktrace ();
- Throw New RuntimeException (
- "Failed in Create preview image. Error: "
- + e.getmessage ());
- }
- }
Multiparthttpservletrequest implementation of file uploads in spring