Jqm file upload, Image Upload, jqm form operations, jqm ajax usage, jqm File Operations Daquan, file operation demo

Source: Internet
Author: User

I recently saw various problems in uploading images or files using html5 in the Forum. I have never done this before. Today I have made some time to study it. Now the example is OK. I will share it with you, hoping to help you.
Well, let's first look at the effect:

Let's see the Page code:

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> Let's take a look at the simple servlet code:

Package com. herman. jqm. servlet; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. fileupload. fileItem; I Mport org. apache. commons. fileupload. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileUpload;/*** @ see: upload an image or file FileServlet * @ author Administrator * @ date 10:00:39 on January 1, June 23, 2014 */public class FileServlet extends HttpServlet {private static final long serialVersionUID = 1L; /*** default constructor */public FileServlet () {}/ *** @ see HttpServlet # doGet (HttpServletRequest request, H Response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) * // @ SuppressWarnings ("unchecked") protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCh AracterEncoding ("UTF-8"); // sets the encoding // gets the disk file entry factory DiskFileItemFactory factory = new DiskFileItemFactory (); // obtain the path to which the file is to be uploaded. String path = getServletContext (). getRealPath ("/"); // if the following two rows are not set, uploading large files will occupy a lot of memory. // you can set a temporary storage room, it may be different from the directory of the final stored file. * ** the principle is that it is first saved to the temporary storage room and then written to the hard disk of the corresponding directory. * When uploading a file, actually, two copies are uploaded, and the first one is. * In tem format, and then write it to the hard disk of the corresponding directory */factory. setRepository (new File (path); // sets the cache size. When the size of the uploaded File exceeds the cache Put it in the temporary storage room factory. setSizeThreshold (1024*1024); // high-level API File upload processing ServletFileUpload upload = new ServletFileUpload (factory ); try {// you can upload multiple file lists <FileItem> List = (list <FileItem>) upload. parseRequest (request); for (FileItem item: list) {// obtain the attribute name of the Form. String name = item. getFieldName (); // if the obtained form information is normal text information if (item. isFormField () {// obtain the String entered by the user. The name is very good because the String value = item is submitted in the form. getString (); request. se TAttribute (name, value);} else {// process input non-simple strings, such as binary images and movies. *** the following three steps, obtain the name of the uploaded file * // obtain the path String value = item. getName (); // index to the last backslash int start = value. lastIndexOf ("\"); // intercept the name of the uploaded file. Add 1 to remove the backslash, String filename = value. substring (start + 1); request. setAttribute (name, filename); // OutputStream out = new FileOutputStream (new File (path, filename) Written manually to the disk; InputStream in = item. getInputStream (); Int length = 0; byte [] buf = new byte [1024]; // in. read (buf) the data read each time is stored in the buf array while (length = in. read (buf ))! =-1) {// extract data from the buf array and write it to the (output stream) disk out. write (buf, 0, length);} in. close (); out. flush (); out. close () ;}} catch (Exception e) {e. printStackTrace ();} request. getRequestDispatcher ("/OK .html "). forward (request, response );}}
It's easy. Well, it's here. You are also welcome to pay more attention to my blog!
If you have any questions, please join the QQ Group 135430763 to learn together !!!
Resource demo download path: http://download.csdn.net/download/xmt1139057136/7538193
Click here to download








Related Article

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.