Implement image upload
Users must be able to upload images, so the ability to upload files is required. The more common file Upload component has Commons FileUpload, here we choose commons FileUpload.
The first is the form of the page form settings, remember to match the method and Enctype properties Oh!!
<form action= "upload.do" method= "post" enctype= "Multipart/form-data" > <input name= "File" type= "File" > <input type= "Submit" value= "Submit" ></form>
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:
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" > <!--set the maximum size of the uploaded 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 Fileaction, HttpServletRequest is transformed into Multiparthttpservletrequest, which makes it very easy to get the file name and the content of the files.
@RequestMapping (value = "/upload", method = Requestmethod.post) public string upload (Httpservletrequest request, Httpservletresponse response) throws IOException, IllegalStateException { // Transformation to multiparthttprequest: MultipartHttpServletRequest multipartRequest = (multiparthttpservletrequest) request ; // Get File: Multipartfile multipartfile = multipartrequest.getfile ("file"); // get filename: string filename =multipartfile.getoriginalfilename (); string tmps[] = filename.split ("\ \"); / "." have special meanings in regular expressions so to escape /** * {1} get input stream write file ( here implement zoom function of picture and interception capabilities ) **/ inputstream input = multipartfile.getinputstream ();// createpreviewimage (Input, "small-" +tmps[0]+ ". jpg"); createpreviewimageofcut (Input, "smallcut-" +tmps[0]+ ". jpg"); &NBSP;&NBSP;&NBSP;&NBSP;/**&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;{2} or: **/ file source = new file ("f://" + "normal-" +tmps[0]+ ". jpg"); //normal size picture multipaRtfile.transferto (source); return "FileUpload"; }
The remaining picture scaling and image interception are all in the following code, which is relatively simple and does not explain too much
//Create thumbnail public void createpreviewimage (InputStream input,string filename) throws IOException { file fo = new file ("f://" +filename); // target picture bufferedimage bis = imageio.read (input); //original picture int w = bis.getwidth (); //Picture Original Size int h = Bis.getheight (); int nw ; //the size of the picture after zooming int nh ; The standard for //thumbnails is 120*120 (equal scale) if (w / h < 1 Double) { nh = 120; nw = nh*w/h; }else { nw = 120; nh = nw*h/w; } bufferedimage _image = new bufferedimage ( NW,&NBSP;NH,BUFFEREDIMAGE.TYPE_INT_RGB); _image.getgraphics (). DrawImage (bis, 0, 0, nw, nh, null); //drawing a reduced figure fileoutputStream newimageout = new fileoutputstream (FO); //output to file stream /* * JPEGImageEncoder Encode the image buffer data as a JPEG data stream. Users of this interface should be in the Raster * or BufferedImage Provide image data in the JPEGEncodeParams object to set the necessary parameters, * and successfully opened the outputstream (encoding JPEG flow intent stream). jpegimageencoder interface * encode image data as an interchangeable thumbnail jpeg The data stream that will be written to the OutputStream provided to the encoder. Note that the classes in the:com.sun.image.codec.jpeg package do not belong to the core java api. They are part of the Sun release JDK and JRE products. While other licensees may choose to publish these classes, developers cannot send expect software to be implemented from non- Sun To get them in. We expect that the same functionality will ultimately be available in the core API or standard expansion show. */ Jpegimageencoder encoder = jpegcodec.createjpegencoder (newimageout); encoder.encode (_image); //near JPEG encoding Newimageout.close (); } //Create intercept map public void createpreviewimageofcut (Inputstream input,string filename) throws ioexception { file fo = new file ("f://" +filename); // target picture bufferedimage bis = imageio.read (input); //Original Image int w&nbsP;= bis.getwidth (); //Picture Original Size Int h = bis.getheight (); //Four parameters are image start coordinates and width height, i.e. cropimagefilter (int x,int y,int Width,int height), please refer to Api imagefilter cropfilter for more information. =new cropimagefilter (0,0,200,200); //Get pictures image croppedimage = toolkit.getdefaulttoolkit (). CreateImage (New filteredimagesource (Bis.getsource (), cropfilter)); //Image Switch to BufferedImage Bufferedimage bufimg = new bufferedimage (Croppedimage.getwidth (NULL), Croppedimage.getheight (NULL), BUFFEREDIMAGE.TYPE_INT_RGB); bufimg .creategraphics (). DrawImage ( croppedimage, 0, 0, null); //Output as Picture file imageio.write (bufimg, "JPEG", &NBSP;FO); }}
This article is from the "meteor Shower It Journey" blog, please be sure to keep this source http://lxy2020.blog.51cto.com/2528961/1568956
Spring File Upload + image zoom + image Capture (Avatar function)