Dynamic upload of multiple files and resolution of file name duplication problem 3 (61), name 61

Source: Internet
Author: User

Dynamic upload of multiple files and resolution of file name duplication problem 3 (61), name 61
Dynamically upload multiple files

<Form name = "xx" action = "<c: url value = '/Up3Servlet'/> "method =" post "enctype =" multipart/form-data "> <table id =" tb "border =" 1 "> <tr> <td> File: </td> <input type = "file" name = "file"> <button onclick = "_ del (this ); "> Delete </button> </td> </tr> </table> <br/> <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"); // write a row var tr = tb. insertRow (); // write column var td = tr. insertCell (); // write data to td. innerHTML = "File:"; // declare a new td var td2 = tr. insertCell (); // write 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); // obtain the subscript var index = tr of tr in the table. rowIndex; // Delete var tb = document. getElementById ("tb"); tb. deleteRow (index);} function _ submit () {// traverse all files with var files = document. getElementsByName ("file"); if (files. length = 0) {alert ("no files can be uploaded"); return false ;}for (var I = 0; I <files. length; I ++) {if (files [I]. value = "") {alert ("no." + (I + 1) + "files cannot be blank"); return false ;}} document. forms ['xx']. submit () ;}</script> 

Traverse all files to be uploaded

Solve the Problem of file name duplication

 

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 response) 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 receives images *. jpg-iamge/g0e ., 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); // obtain the extended String extName = fileName. substring (fileName. lastIndexOf (". "));//. jpg // UUID String uuid = UUID. randomUUID (). toString (). replace ("-", ""); // new name: String newName = uuid + extName; // use UUID to generate a new folder name. 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 ();}}}

 

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.