"Java Technology drip"--streamline the implementation of image processing

Source: Internet
Author: User



Introduction:

For the image processing is used in many projects, such as the General personnel Management system, processing methods are divided into two major categories:

1. The picture file is stored with the disk, only the corresponding file name is saved in the database;

2. The file is stored in a binary form in the database.

The second scenario occupies a large space, and the operation of the binary stream is also more resource-intensive, so the first approach to deal with the situation is more common, commonly used in general system applications, the ongoing DRP The project uses the image upload, the display function processing, uses the Commons-fileupload 1.1.1 version, can implement multi-file upload function.

Realize:

introduce relevant Jar after the package, the method is streamlined, simple implementation of File upload function:

public class Fileuploadservlet {private File uploadpath;    Private File TempPath; public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception{ Diskfileitemfactory factory = new Diskfileitemfactory (); Factory.setsizethreshold (4096); Factory.setrepository ( TempPath); String ItemNo = ""; Servletfileupload upload = new Servletfileupload (factory);//Set Upload file maximum Upload.setsizemax (1000000 *); try{// Gets the collection information within the submission form list fileitems = upload.parserequest (request);//iterates through the elements within the set for (Iterator iter = Fileitems.iterator ();                        Iter.hasnext ();) {Fileitem item = (Fileitem) iter.next ()//is a normal form input domain if (Item.isformfield ()) {///non-committed file data processing }//ignores all other form information that is not a file field if (!item.isformfield ()) {String fileName = Item.getname (); Long size = Item.getsize (); if (filename= =null| | Filename.equals ("")) && size==0) continue;item.write (New File (Uploadpath, FileName);}}} catch (Exception e) {e.printstacktrace ();}} Initialize path information: storage path, temporary storage path public void init () throws ServletException {uploadpath = new File (Getservletcontext (). Getrealpath ("upload")); if (!uploadpath.exists ()) {    Uploadpath.mkdir (); Establish the corresponding upload folder}temppath = new File (Getservletcontext (). Getrealpath ("tmp")); if (!temppath.exists ()) {Temppath.mkdir () ;}}}


Attention:

1. the form in which the file is uploaded must be submitted by Post

2. upload the file's form Add property enctype= "Multipart/form-data"-- multi-segment data submission format (including text format)

3. the method of obtaining the property value using request.getparameter () will be invalidated, and the data must be obtained using the encapsulated method within the component.

4. the file upload path directory implemented by the servlet class is determined when the servlet is initialized, so the path is created dynamically.

Summary:

Image processing to the server, for the display of pictures (or files), the corresponding database table can be added field store file name information, read the picture when the file name, stitching path can be displayed, which requires the implementation of the upload function, but also to achieve the update of the database records

In the image processing function, there are many third-party components, in a simple application of the premise of understanding, for the need for specialized, professional image processing projects, there will be a dedicated image server to use.

Reference:

Download

Picture Storage Architecture Learning: Stand-alone picture server, give love a separate space

"Java Technology drip"--streamline the implementation of image processing

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.