Multiparthttpservletrequest implementation of file uploads in spring

Source: Internet
Author: User
Tags cos

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
    1. <bean id= "Multipartresolver"
    2. class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
    3. <!--set the maximum size of the upload file to 1MB
    4. <property name= "Maxuploadsize" >
    5. <value>1048576</value>
    6. </property>
    7. </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
  1. Public Modelandview HandleRequest (HttpServletRequest request,
  2. HttpServletResponse response) throws Exception {
  3. Transition to Multiparthttprequest:
  4. Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
  5. Get the file:
  6. Multipartfile file = multipartrequest.getfile ("file");
  7. Get file name:
  8. String filename = File.getoriginalfilename ();
  9. Get input stream:
  10. InputStream input = File.getinputstream ();
  11. Write file
  12. Or:
  13. File Source = new file (localfilename.tostring ());
  14. Multipartfile.transferto (source);
  15. }

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
  1. Public Static void createpreviewimage (string srcfile, String destfile) {
  2. Try {
  3. File fi = new file (srcfile); Src
  4. File fo = new file (destfile); Dest
  5. BufferedImage bis = imageio.read (FI);
  6. int w = bis.getwidth ();
  7. int h = bis.getheight ();
  8. Double scale = (double) w/h;
  9. int NW = image_size; Final int image_size = 120;
  10. int nh = (NW * h)/w;
  11. if (NH > Image_size) {
  12. NH = image_size;
  13. NW = (NH * w)/h;
  14. }
  15. Double sx = (double) nw/w;
  16. double sy = (double) nh/h;
  17. Transform.settoscale (SX, SY);
  18. Affinetransformop ato = new affinetransformop (transform, null);
  19. BufferedImage bid = new bufferedimage (NW, NH,
  20. BUFFEREDIMAGE.TYPE_3BYTE_BGR);
  21. Ato.filter (bis, Bid);
  22. Imageio.write (BID, "jpeg", FO);
  23. } catch (Exception e) {
  24. E.printstacktrace ();
  25. Throw New RuntimeException (
  26. "Failed in Create preview image. Error: "
  27. + e.getmessage ());
  28. }
  29. }

Multiparthttpservletrequest implementation of file uploads in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.