Java implementation dynamically upload multiple files and solve the problem of duplicate file name _java

Source: Internet
Author: User
Tags uuid

This article is divided into two major areas to explain:

One, Java implementation dynamic upload multiple files

Second, solve file renaming problem Java

For your reference, the specific contents are as follows

1. Upload Multiple files dynamically

 <form name= "xx" action= "<c:url value= '/up3servlet '/>" method= "post" enctype= "Multipart/form-data" > < Table id= "TB" border= "1" > <tr> <td> File: </td> <td> < Input type= "file" name= "file" > <button onclick= "_del (this);" > Delete </button> </td> </tr> </table> <input type= "button" onclick= " _submit (); "value=" Upload "> <input onclick=" _add (); "type=" button "value=" Add "> </form> </body> <
     Script type= "Text/javascript" > Function _add () {var TB = document.getElementById ("TB");
     Writes a line of var tr = Tb.insertrow ();
      Write column var td = Tr.insertcell ();
     Write Data td.innerhtml= "File:";
     Then declare a new TD var TD2 = Tr.insertcell (); Writes an input td2.innerhtml= ' <input type= ' file ' name= ' file '/><button onclick= ' _del (this);
   > Delete </button> '; } function _del (btn) {var tR = Btn.parentNode.parentNode;
     alert (tr.tagname);
     Gets the subscript var index = Tr.rowindex of TR in the table;
     Delete var TB = document.getElementById ("TB");
   Tb.deleterow (index);
     function _submit () {//traversal of the file var files = document.getelementsbyname ("file");
       if (files.length==0) {alert ("There is no file to upload");
     return false;
         for (Var i=0;i<files.length;i++) {if (files[i].value== ") {alert (" First "+ (i+1) +" file cannot be empty ");
       return false;
   } document.forms[' xx '].submit ();
 } </script>  

Traverse all files to be uploaded

2, solve the problem of duplicate file name

Package cn.hx.servlet;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.UUID;
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.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;

Import Org.apache.commons.io.FileUtils; public class Upimgservlet extends HttpServlet {public void DoPost (HttpServletRequest request, HttpServletResponse resp
    Onse) throws Servletexception, IOException {request.setcharacterencoding ("UTF-8");
    String path = Getservletcontext (). Getrealpath ("/up");
    Diskfileitemfactory disk = new Diskfileitemfactory (1024*10,new File ("d:/a"));
    Servletfileupload up = new Servletfileupload (disk); try{List<fileitem> list = up.parserequest (request);
      Only receive picture *.jpg-iamge/jpege.,bmp/imge/bmp,png, list<string> IMGs = new arraylist<string> (); for (Fileitem file:list) {if (File.getcontenttype (). Contains ("image/")) {String fileName = File.getname ()
          ;
          
          FileName = filename.substring (Filename.lastindexof ("\") +1); Gets the extension String extname = filename.substring (Filename.lastindexof ("."));
          /.jpg//uuid String UUID = Uuid.randomuuid (). toString (). Replace ("-", "");     New name String newName = uuid+extname;
              Here you use the UUID to generate a new folder name so that it does not cause duplicate names Fileutils.copyinputstreamtofile (File.getinputstream),
          New File (path+ "/" +newname));
        Put it in List Imgs.add (NewName);
      } file.delete ();
      } request.setattribute ("IMGs", IMGs);
    Request.getrequestdispatcher ("/jsps/imgs.jsp"). Forward (request, response);}catch (Exception e) {e.printstacktrace ();
 }
  
  }

}

The above implementation of the Java file upload, to solve the problem of duplicate file name, I hope to help you learn.

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.