java實現(xiàn)動態(tài)上傳多個文件并解決文件重名問題
更新時間:2016年03月15日 11:59:31 作者:鄭宏鑫erke
這篇文章主要為大家詳細介紹了java實現(xiàn)動態(tài)上傳多個文件,并解決文件重名問題的方法,感興趣的小伙伴們可以參考一下
本文分為兩大方面進行講解:
一、java實現(xiàn)動態(tài)上傳多個文件
二、解決文件重命名問題java
供大家參考,具體內容如下
1、動態(tài)上傳多個文件
<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);">刪除</button> </td> </tr> </table> <br/> <input type="button" onclick="_submit();" value="上傳"> <input onclick="_add();" type="button" value="增加"> </form> </body> <script type="text/javascript"> function _add(){ var tb = document.getElementById("tb"); //寫入一行 var tr = tb.insertRow(); //寫入列 var td = tr.insertCell(); //寫入數(shù)據(jù) td.innerHTML="File:"; //再聲明一個新的td var td2 = tr.insertCell(); //寫入一個input td2.innerHTML='<input type="file" name="file"/><button onclick="_del(this);">刪除</button>'; } function _del(btn){ var tr = btn.parentNode.parentNode; //alert(tr.tagName); //獲取tr在table中的下標 var index = tr.rowIndex; //刪除 var tb = document.getElementById("tb"); tb.deleteRow(index); } function _submit(){ //遍歷所的有文件 var files = document.getElementsByName("file"); if(files.length==0){ alert("沒有可以上傳的文件"); return false; } for(var i=0;i<files.length;i++){ if(files[i].value==""){ alert("第"+(i+1)+"個文件不能為空"); return false; } } document.forms['xx'].submit(); } </script> </html>
遍歷所有要上傳的文件
2、解決文件的重名的問題
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); //只接收圖片*.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); //獲取擴展 String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg //UUID String uuid = UUID.randomUUID().toString().replace("-", ""); //新名稱 String newName = uuid+extName; //在這里用UUID來生成新的文件夾名字,這樣就不會導致重名 FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path+"/"+newName)); //放到list imgs.add(newName); } file.delete(); } request.setAttribute("imgs",imgs); request.getRequestDispatcher("/jsps/imgs.jsp").forward(request, response); }catch(Exception e){ e.printStackTrace(); } } }
以上實現(xiàn)了java多文件上傳,解決了文件重名問題,希望對大家的學習有所幫助。
相關文章
詳解Java利用實現(xiàn)對稱加密(DES、3DES、AES)
本篇文章主要介紹了Java利用實現(xiàn)對稱加密(DES、3DES、AES),具有一定的參考價值,有興趣的可以了解一下。2017-01-01Java8新特性之深入解析日期和時間_動力節(jié)點Java學院整理
這篇文章主要介紹了Java8新特性之深入解析日期和時間_動力節(jié)點Java學院整理,需要的朋友可以參考下2017-06-06Java排序之Comparable和Comparator比較器詳解
這篇文章主要介紹了Java排序之Comparable和Comparator比較器詳解,Comparable<T>是內部比較器,Comparator<T>是外部比較器,最推薦使用Comparator<T>接口排序,Comparator提供靜態(tài)方法很方便,推薦使用,需要的朋友可以參考下2024-01-01使用spring oauth2框架獲取當前登錄用戶信息的實現(xiàn)代碼
這篇文章主要介紹了使用spring oauth2框架獲取當前登錄用戶信息的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07