本文分為兩大方面進行講解:
一、java實現(xiàn)動態(tài)上傳多個文件
二、解決文件重命名問題java
供大家參考,具體內(nèi)容如下
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中的下標(biāo) 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("http://")+1); //獲取擴展 String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg //UUID String uuid = UUID.randomUUID().toString().replace("-", ""); //新名稱 String newName = uuid+extName; //在這里用UUID來生成新的文件夾名字,這樣就不會導(dǎo)致重名 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多文件上傳,解決了文件重名問題,希望對大家的學(xué)習(xí)有所幫助。
新聞熱點
疑難解答
圖片精選