本文分為兩大方面進行講解:
一、java實現動態上傳多個文件
二、解決文件重命名問題java
供大家參考,具體內容如下
1、動態上傳多個文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
< 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(); //寫入數據 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、解決文件的重名的問題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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(); } } } |
以上實現了java多文件上傳,解決了文件重名問題,希望對大家的學習有所幫助。