具體代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
|
***web.xml*** <?xml version= "1.0" encoding= "utf-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "webapp_id" version= "2.5" > <servlet> <servlet-name>fileupload</servlet-name> <servlet- class >cn.com.action.fileupload</servlet- class > </servlet> <servlet-mapping> <servlet-name>fileupload</servlet-name> <url-pattern>/fileupload</url-pattern> </servlet-mapping> </web-app> |
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package cn.com.action; import java.io.file; import java.io.*; import java.io.ioexception; import java.util.list; 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.fileuploadexception; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; /** * * @author administrator * 文件上傳 * 具體步驟: * 1)獲得磁盤文件條目工廠 diskfileitemfactory 要導包 * 2) 利用 request 獲取 真實路徑 ,供臨時文件存儲,和 最終文件存儲 ,這兩個存儲位置可不同,也可相同 * 3)對 diskfileitemfactory 對象設置一些 屬性 * 4)高水平的api文件上傳處理 servletfileupload upload = new servletfileupload(factory); * 目的是調用 parserequest(request)方法 獲得 fileitem 集合list , * * 5)在 fileitem 對象中 獲取信息, 遍歷, 判斷 表單提交過來的信息 是否是 普通文本信息 另做處理 * 6) * 第一種. 用第三方 提供的 item.write( new file(path,filename) ); 直接寫到磁盤上 * 第二種. 手動處理 * */ public class fileupload extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding( "utf-8" ); //設置編碼 //獲得磁盤文件條目工廠 diskfileitemfactory factory = new diskfileitemfactory(); //獲取文件需要上傳到的路徑 string path = request.getrealpath( "/upload1" ); // string path = "c:/upload1"; //如果沒以下兩行設置的話,上傳大的 文件 會占用 很多內存, //設置暫時存放的 存儲室 , 這個存儲室,可以和 最終存儲文件 的目錄不同 /** * 原理 它是先存到 暫時存儲室,然后在真正寫到 對應目錄的硬盤上, * 按理來說 當上傳一個文件時,其實是上傳了兩份,第一個是以 .tem 格式的 * 然后再將其真正寫到 對應目錄的硬盤上 */ factory.setrepository( new file(path)); //設置 緩存的大小,當上傳文件的容量超過該緩存時,直接放到 暫時存儲室 factory.setsizethreshold( 1024 * 1024 ) ; //高水平的api文件上傳處理 servletfileupload upload = new servletfileupload(factory); try { //可以上傳多個文件 list<fileitem> list = (list<fileitem>)upload.parserequest(request); for (fileitem item : list){ //獲取表單的屬性名字 string name = item.getfieldname(); //如果獲取的 表單信息是普通的 文本 信息 if (item.isformfield()){ //獲取用戶具體輸入的字符串 ,名字起得挺好,因為表單提交過來的是 字符串類型的 string value = item.getstring() ; request.setattribute(name, value); } else { //對傳入的非 簡單的字符串進行處理 ,比如說二進制的 圖片,電影這些 /** * 以下三步,主要獲取 上傳文件的名字 */ //獲取路徑名 string value = item.getname() ; //索引到最后一個反斜杠 int start = value.lastindexof( "\\" ); //截取 上傳文件的 字符串名字,加1是 去掉反斜杠, string filename = value.substring(start+ 1 ); request.setattribute(name, filename); //真正寫到磁盤上 //它拋出的異常 用exception 捕捉 //item.write( new file(path,filename) );//第三方提供的 //手動寫的 outputstream out = new fileoutputstream( new file(path,filename)); inputstream in = item.getinputstream() ; int length = 0 ; byte [] buf = new byte [ 1024 ] ; system.out.println( "獲取上傳文件的總共的容量:" +item.getsize()); // in.read(buf) 每次讀到的數據存放在 buf 數組中 while ( (length = in.read(buf) ) != - 1 ){ //在 buf 數組中 取出數據 寫到 (輸出流)磁盤上 out.write(buf, 0 , length); } in.close(); out.close(); } } } catch (fileuploadexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } request.getrequestdispatcher( "filedemo.jsp" ).forward(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
***index.jsp*** <%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>上傳入口</title> </head> <body> <form action= "fileupload" enctype= "multipart/form-data" method= "post" > 用戶名稱:<input type= "text" name= "usename" > <br/> 上傳圖片:<input type= "file" name= "file1" ><br/> 上傳文件:<input type= "file" name= "file2" ><br/> <input type= "submit" value= "提交" /> </form> </body> </html> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
***filedemo.jsp*** <%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>上傳文件圖片案列</title> </head> <body> 用戶名稱:${requestscope.usename }<br /> 圖片名稱:${requestscope.file1 }<br /> 文件名稱:${requestscope.file2 }<br /> <!-- 把上傳的圖片顯示出來 --> <img alt= "go" src= "upload1/<%=(string) request.getattribute(" file1 ")%> " /> </body> </html> |
總結
以上所述是小編給大家介紹的java實現上傳文件圖片到指定服務器目錄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/u014108448/article/details/46804729