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 ();}}}