Using Uploadify for file uploads

Source: Internet
Author: User
Tags rar

Using Uploadify for file uploads

  1. Import the required js,css and other files

  2. Add uploadify.jsp File

    Code

    <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding= "UTF-8"%>
    <%
    String path = Request.getcontextpath ();
    String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
    %>
    <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
    <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
    <title>insert title here</title>
    <base href= "<%=basePath%>" >
    <link href= "<%=basepath%>static/js/uploadify/uploadify.css" rel= "stylesheet" type= "Text/css" >
    <script type= "Text/javascript" src= "<%=basepath%>static/js/uploadify/jquery-1.4.2.min.js" ></ Script>
    <script type= "Text/javascript" src= "<%=basepath%>static/js/uploadify/swfobject.js" ></script>
    <script type= "Text/javascript" src= "<%=basepath%>static/js/uploadify/jquery.uploadify.v2.1.4.min.js" ></script>
    <body>
    <div id= "Filequeue" ></div>
    <label> Pictures: </label>
    <div class= "Showimg" >

    </div>
    <input type= "File" Name= "uploadify" id= "uploadify" style= "width:200px;" />
    <p><a href= "javascript:jquery (' #uploadify '). Uploadifyupload ()" > Start uploading </a></p>
    </body>
    <script type= "Text/javascript" >
    Official website: http://www.uploadify.com/
    $ (document). Ready (function () {
    $ ("#uploadify"). Uploadify ({
    ' Uploader ': "<%=basepath%>static/js/uploadify/uploadify.swf",
    ' Script ': "<%=basepath%>uploadfile",//here is the servlet request path, and if the request uses uploadfile.jsp, here is the path to uploadfile.jsp "<%=basepath%>static/js/uploadify/ Uploadfile.jsp "
    ' Cancelimg ': "<%=basepath%>static/js/uploadify/cancel.png",
    ' Folder ': "Static/uploads",//upload the file to store the path, please keep the same value as the path in uploadfile.jsp
    ' Queueid ': "Filequeue",
    ' Queuesizelimit ': 10,//limit the number of uploaded files
    ' Fileext ': "*.rar,*.zip",
    ' Filedesc ': "RAR *.rar",//Restricted file type
    ' Auto ': false,
    ' Multi ': true,//allow multiple file uploads
    ' Simuploadlimit ': 2,//the number of simultaneous runs of the uploaded process
    ' ButtonText ': "Select Files",
    ' OnComplete ': function (event, ID, Fileobj, response, data) {
    Response return Value: Renamed file name, File save path
    var resultarray = Response.split (",");
    var realname = resultarray[0];//The name of the picture now, preceded by the picture name with the time, with the extension
    Realname = $.trim (realname);
    var p = "<%=basepath%>static/uploads/" +realname;
    $ (". Showimgs"). attr ("src", p);
    }
    });
    });
    </script>

  3. Upload file: Add Servlet method (or use uploadfile.jsp)

    (1)//servlet method code

    @RequestMapping ("UploadFile")
    @ResponseBody
    Public String Uploadifystart (httpservletrequest request, httpservletresponse response)
    Throws Servletexception, ioexception{
    String Savepath = Request.getsession (). Getservletcontext (). Getrealpath ("/");
    String PATH = "/static/uploads/";
    Savepath = Savepath + PATH;
    File F1 = new file (Savepath);
    System.out.println (Savepath);
    if (!f1.exists ()) {
    F1.mkdirs ();
    }

    Diskfileitemfactory FAC = new Diskfileitemfactory ();
    Servletfileupload upload = new Servletfileupload (FAC);
    Upload.setheaderencoding ("Utf-8");
    List fileList = null;
    try {
    FileList = upload.parserequest (request);
    } catch (Fileuploadexception ex) {
    Return "";
    }
    Iterator<fileitem> it = Filelist.iterator ();
    String name = "";
    String extname = "";
    while (It.hasnext ()) {
    Fileitem item = It.next ();
    if (!item.isformfield ()) {
    Name = Item.getname ();
    Long size = Item.getsize ();
    String type = Item.getcontenttype ();
    SYSTEM.OUT.PRINTLN (Size + "" + type);
    if (name = = NULL | | Name.trim (). Equals ("")) {
    Continue
    }

    Extension format:
    if (Name.lastindexof (".") >= 0) {
    Extname = name.substring (Name.lastindexof ("."));
    }

    File file = null;
    do {
    Generate file Name:
    Name = Uuid.randomuuid (). toString ();
    File = new file (Savepath + name + extname);
    } while (File.exists ());

    File SaveFile = new file (Savepath + name + extname);
    try {
    Item.write (SaveFile);
    } catch (Exception e) {
    E.printstacktrace ();
    }
    }
    }
    return name + Extname;
    }

    (2) uploadfile.jsp implementation upload

    uploadfile.jsp Code

    <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>
    <%@ page import= "java.io.*, java.util.*, org.apache.commons.fileupload.*"%>
    <%@ page import= "org.apache.commons.fileupload.disk.*, org.apache.commons.fileupload.servlet.*"%>
    <%!

    String PATH = "/static/uploads/";
    public void upload (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
    String Savepath = This.getservletconfig (). Getservletcontext (). Getrealpath ("");
    Savepath = Savepath + PATH;
    File F1 = new file (Savepath);
    System.out.println (Savepath);
    if (!f1.exists ()) {
    F1.mkdirs ();
    }

    Diskfileitemfactory FAC = new Diskfileitemfactory ();
    Servletfileupload upload = new Servletfileupload (FAC);
    Upload.setheaderencoding ("Utf-8");
    List fileList = null;
    try {
    FileList = upload.parserequest (request);
    } catch (Fileuploadexception ex) {
    Return
    }
    Iterator<fileitem> it = Filelist.iterator ();
    String name = "";
    String extname = "";
    while (It.hasnext ()) {
    Fileitem item = It.next ();
    if (!item.isformfield ()) {
    Name = Item.getname ();
    Long size = Item.getsize ();
    String type = Item.getcontenttype ();
    SYSTEM.OUT.PRINTLN (Size + "" + type);
    if (name = = NULL | | Name.trim (). Equals ("")) {
    Continue
    }

    Extension format:
    if (Name.lastindexof (".") >= 0) {
    Extname = name.substring (Name.lastindexof ("."));
    }

    File file = null;
    do {
    Generate file Name:
    Name = Uuid.randomuuid (). toString ();
    File = new file (Savepath + name + extname);
    } while (File.exists ());

    File SaveFile = new file (Savepath + name + extname);
    try {
    Item.write (SaveFile);
    } catch (Exception e) {
    E.printstacktrace ();
    }
    }
    }
    Response.getwriter (). Print (name + extname);
    }
    %>
    <%
    Upload (request, response);
    %>

Using Uploadify for file uploads

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.