JQuery upload plugin UPLOADIFY No refresh upload file


UPLOADIFY V3.2.1 Upload Image and Preview


Front end JSP:

  $ ("# UPLOAD_ORG_CODE"). Uploadify ({'Height': 27, 'Width': 80, 'ButtOnTontext': 'Select the picture', 'SWF': '$ {pageContext. request.contextPath} /js/uploadify/uploadify.swf ',' uploader ':' $ {pageContext.request.contextPath} / uploadIMGSerlet ',' auto ': true,' multi ': false,' removeCompleted ': false,' Cancelimg ':' $ {pagecontext.request.contextpath} /js/uploadify/uploadify-cancel.png ',' FileTypeextS ':' * .jpg; *. jpge; *. gif; *. png ',' filesizelimit ':' 2MB ',' Onuploadsuccess': Function (File, Data, Response) {$ ('#' + file. ID) .find ('. Data'). HTML (''); $ ("# UPLOAD_ORG_CODE_NAME"). VAL (DATA); $ ("# UPLOAD_ORG_CODE_IMG"). Attr ("src", "$ {pageContext.Request .contextPath} / getimg? file = "+ data); $ (" # UPLOAD_ORG_CODE_IMG "). show ();}, // plus this sentence to overSelectEnRROR method [Event to rewrite] 'OverrideEvents': [' OnSelectElectError ',' OnDialogClose '], // Returns an error, trigger' onselectError ': Function (File, ErrorCode, ErrorMSG) {switch (ERRORCODE) {CASE-110:Alert ("File [" + File.Name + "] size beyond system restrictions" + jQuery ('# UPLOAD_ORG_CODE'). Uploadify ('settings', 'filesizelimit') + "Size!"); Break; Case -120: Alert ("file [" + file.name + "] size exception!"); Break; case -130: alert ("file [" + file.name + "] type is incorrect!"); Break;}},})    


Package com.mybank.enterprise.framework. Servlet; import java.io.file; import java.io.fileoutputStream; import java.io ioException; import java.io.inputstream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; 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; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons .fileupload.servlet.ServletFileUpload; import com.mybank.enterprise.util.Constant; import com.mybank.enterprise.util.StringUtil; public class UploadIMGSerlet extends HttpServlet {private static final long serialVersionUID = 1L; // save the path file upload Private string configPath = constant.rb.getstring (“img_path”); // Temporary textPath member private String dirTemp = “resource / temp /”; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException IoException {string ret_filename = null; // Return to the front-end modified picture name Request.setCharacterencoding (“UTF-8”); response.setContentType (“text / html; charSet = UTF-8”); PrintWriter Out = response .Getwriter (); // file save directory path string savePath = configPath; // Temporary file directory string temppath = this.getServletContext (). getRealPath (“/”) + Dirtemp; // Create Folder File Dirfile = New File SavePath); if (! Dirfile.exists ()) {dirFile.mkdirs ();} // Create a temporary folder File dirTempFile = new File (tempPath); if (dirTempFile.exists (!)) {dirTempFile.mkdirs ();} DiskFileItemFactory factory = new DiskFileItemFactory (); Factory.setSizTHRESHOLD (20 * 1024 * 1024); // When you use memory more than 5m, a temporary file is generated and stored in a temporary directory. Factory.setRepository (New file (temppath)); // Set the directory where the temporary file is stored. ServletFileUpload upload = new ServletFileUpload (factory); upload.setHeaderEncoding ( “UTF-8”); try {List
 items = upload.parseRequest (request); Iterator  itr = items.iterator (); while (itr .hasnext ()) {fileitem item = (fileItem) itr.next (); string filename = item.getname (); if (filename! = null) {string endstr = filename.substring ("."), filename.length (); filename = stringutil.createserial20 (). Concat (endstr); RET_FILENAME = fileName;} if {try {File uploadedFile = new File (savePath, fileName) (item.isFormField ()!); OutputStream os = new FileOutputStream (uploadedFile); InputStream is = item.getInputStream (); byte buf [] = new Byte [1024]; // can modify 1024 to improve read speed int LENGTH = 0; while ((Length = is.read (buf))> 0) {os.write (buf, 0, length);} // Close the stream Os.Flush (); os.close (); is.close ();} catch (exception e) {E.PrintStackTrace ();}}}} catch (fileuploadexception e) {E.PrintStackTrace );} // returns the modified picture name to the front end.print (RET_FILENAME); out.clush (); out.close ();}}    


Show picture servlet

Package com.mybank.enterprise.framework.servlet; import java.io.file; import java. IO.fileinputStream; import java.io ioException; import java.io.outputstream; import javax.servlet.servletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mybank.enterprise.util.Constant; public class GetIMGServlet extends HttpServlet {private static final long serialVersionUID = 2761789171087122738L; public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost (req, resp);} @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String file = req.getParameter ( “file “); File pic = new file (constant.rb.getstring (” IMG_PATH “) + file); fileinputstream FIS = New FileInputStream (PIC); OutputStream OS = Resp.GetouTPUTSTREAM (); try {int count = 0; byte [] buffer = new byte [1024 * 1024]; while (count = fis.read (buffer))! = -1) Os.Write (buffer, 0, count) Os.flush ();} catch (ioException e) {E.PrintStackTrace ();} finally {if (os! = Null) Os.close (); if (fis! = Null) fis.close (); }}}
   
The IMG tag can point to the servlet as long as it returns the flow object, Commonly used, such as verification code


Reference article:


http://www.cnblogs.com/yangy608/p/3915337.html

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment