Web apps generate thumbnails for uploading pictures is a common basic function, through thumbnail generation to improve the performance of information browsing, in order to ensure the user experience and reduce the amount of data transmission. This is an example of how you can use Java to achieve proportional thumbnail image generation.
Effect View
Code Writing
Thumbnailator is a more streamlined thumbnail generation library for the Java interface. By simplifying the thumbnail process from the API providing thumbnails of existing image files and image objects, two or three lines of code can generate thumbnails from existing images and allow fine-tuning of thumbnail generation while preserving the amount of code that needs to be written to the minimum.
1. Import the relevant packages
2. Configure Web. xml
Xml
<?xml version= "1.0" encoding= "UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_ 3_1.xsd "id=" webapp_id " version=" 3.1 "> <display-name>Thumbnail</display-name> <!--configuration Springmvc dispatcherservlet-- <servlet> <servlet-name>Dispatcherservlet</servlet-name> <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>Contextconfiglocation</param-name> <param-value>Classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Dispatcherservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></Web-app>
3. Configure Springmvc.xml
Springmvc.xml
<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns="Http://www.springframework.org/schema/beans" Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" Xmlns:context="Http://www.springframework.org/schema/context" Xmlns:mvc="Http://www.springframework.org/schema/mvc" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd "> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <!--Configure a custom scan of the package-- <context:component-scan base-package="Com.wenteryan"></context:component-scan> <!--configuration View resolver: How to resolve the handler method return value to the actual physical view -- <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name="prefix" value="/"></Property > < property name="suffix" value=". jsp"></Property > < property name= "viewclass" value=" Org.springframework.web.servlet.view.JstlView "></Property > </Bean> <bean id= "multipartresolver"class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "> < property name="defaultencoding" value="UTF-8"></Property > < property name="Maxuploadsize" value="10485760000" ></Property > < property name="Maxinmemorysize" value="40960" ></Property > </Bean></Beans>
4. Write action
Thumbnailaction.java
@Controller Public class thumbnailaction { PublicUploadservice Uploadservice; PublicThumbnailservice Thumbnailservice;@RequestMapping(value="/thumbnail", Method=requestmethod.post) PublicModelandviewthumbnail(@Requestparam("image") Commonsmultipartfile file, HttpSession session)throwsException {String Uploadpath ="/images"; String Realuploadpath = Session.getservletcontext (). Getrealpath (Uploadpath); String imageUrl = uploadservice.uploadimage (file, Uploadpath, Realuploadpath); String Thumbimageurl = thumbnailservice.thumbnail (file, Uploadpath, Realuploadpath); Modelandview ret =NewModelandview (); Ret.addobject ("Imagesurl", IMAGEURL); Ret.addobject ("Thumbnailurl", Thumbimageurl); Ret.setviewname ("thumbnail");returnRET; }@Autowired Public void Setuploadservice(Uploadservice Uploadservice) { This. Uploadservice = Uploadservice; }@Autowired Public void Setthumbnailservice(Thumbnailservice Thumbnailservice) { This. Thumbnailservice = Thumbnailservice; }}
5. Write the service
Thumbnailservice. java
@Service Public class thumbnailservice { Public Static Final intWIDTH = -; Public Static Final intHEIGHT = -; PublicStringthumbnail(commonsmultipartfile file, String Uploadpath, String realuploadpath) {Try{String des = Realuploadpath +"/thum_"+ File.getoriginalfilename (); Thumbnails.of (File.getinputstream ()). Size (WIDTH, HEIGHT). ToFile (DES); }Catch(Exception e) {E.printstacktrace (); }returnUploadpath +"/thum_"+ File.getoriginalfilename (); }}
Uploadservice. java
@Service Public classUploadservice { PublicStringUploadimage(commonsmultipartfile file, String Uploadpath, String realuploadpath) {InputStream is=NULL; OutputStream OS =NULL;Try{ is= File.getinputstream (); String des = Realuploadpath +"\\"+ File.getoriginalfilename (); OS =NewFileOutputStream (DES);byte[] buffer =New byte[1024x768] ;intLen =0; while((len= is. Read (buffer)) >0) {os.write (buffer); } }Catch(Exception e) {E.printstacktrace (); }finally{if( is!=NULL) {Try{ is. Close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }if(os!=NULL) {Try{Os.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } } }returnUploadpath +"/"+ File.getoriginalfilename (); }}
6. Writing a JSP file
index.jsp
<div class="Panel panel-warning"> <div class="panel-heading"><h2>Java implementation of picture-scale thumbnail image</H2></div> <div class="Panel-body"> <form Action="thumbnail" method="POST" enctype ="Multipart/form-data"> <h2>Please select the uploaded picture</H2> <div class="Form-group"> <input type="file" name="Image " id="image" / > </div> <div class="Form-group"> <button class="btn btn-success" type="Submit"> Start uploading</button> </div> </form> </div></div>
thumbnail.jsp
<div class="Panel panel-warning"> <div class="panel-heading"><h2>Original picture and thumbnail image</H2></div> <div class="Panel-body"> <img alt= "" src="${pagecontext.request.contextpath}${imagesurl } "/> <img alt= "" src="${pagecontext.request.contextpath}${ Thumbnailurl} "/> <br><br> <a class="btn btn-warning" href="${pagecontext.request.contextpath } ">Return</a> </div></div>
Technical Summary
The benefits of implementing picture thumbnails are summarized below:
1, save storage space.
2, more flexible response to the operation of the multi-dimensional picture requirements.
3, improve the program performance and efficiency.
Java implementation Picture Scale thumbnail (thumbnailator + jsp+springmvc)