本文實(shí)例為大家分享了servlet上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
1.servlet上傳文件
servlet上傳文件就是將客戶端的文件上傳到服務(wù)器端。
向服務(wù)器發(fā)送數(shù)據(jù)時(shí),客戶端發(fā)送的http請求正文采用“multipart/form-data”數(shù)據(jù)類型,他表示復(fù)雜的多個(gè)子部分的復(fù)合表單。
為了簡化“multipart/form-data”數(shù)據(jù)的處理過程。可以使用Apache組織提供是的兩個(gè)開源包來來實(shí)現(xiàn)上傳。
fileupload軟件包(commons-fileupload-1.2.1.jar),負(fù)責(zé)文件上傳的軟件包。
io軟件包(commons-io-1.4.jar)負(fù)責(zé)輸入輸出的軟件包。
2.servlet上傳文件相關(guān)類
2.1fileupload軟件包把請求正文包含的復(fù)合表單的每個(gè)子部分看做FileItem對象。FileItem對象分為兩種類型。
(1)formFiled:普通表單域類型,如表單中的文本和按鈕等。
(2)非formFiled:上傳文件類型,表單中的文件域就是這種類型。
2.2FileItemFactory接口和FileItem接口
FileItemFactory 是創(chuàng)建FileItem 對象的工廠。
DiskFileItemFactory 實(shí)現(xiàn)了FileItemFactory接口,DiskFileItemFactory用于創(chuàng)建DiskFileItem對象。
DiskFileItem對象用于把客戶端上傳的文件保存在客戶端。
2.3ServletFileUpload 類
ServletFileUpload為文件上傳處理器。和DiskFileItemFactory對象關(guān)聯(lián)。
3.上傳文件案例
上傳文件頁面(upload.html)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Title</ title > </ head > < body > < form action = "upload" enctype = "multipart/form-data" method = "POST" > < input name = "username" size = "30" > < input type = "file" name = "file1" size = "30" > < input type = "file" name = "file2" size = "30" > < input type = "submit" name = "submit" value = "upload" > </ form > </ body > </ html > |
上傳文件的servlet類
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package com.learn; 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; import org.apache.poi.common.usermodel.LineStyle; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; public class ServletUpload extends HttpServlet { private String filePath; private String tempFilePath; @Override public void init(ServletConfig config) throws ServletException { super .init(config); //獲取類文件路徑初始化值 filePath = config.getInitParameter( "filePath" ); tempFilePath = config.getInitParameter( "tempFilePath" ); System.out.println( "init filepath:" +filePath); System.out.println( "tempFilePath:" +tempFilePath); //獲取文件路徑真實(shí)值 filePath = config.getServletContext().getRealPath(filePath); tempFilePath = config.getServletContext().getRealPath(tempFilePath); System.out.println( "realPath:" +filePath); System.out.println( "tempFilePath" +tempFilePath); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super .doPost(req, resp); //設(shè)置返回內(nèi)容為純文字內(nèi)容 resp.setContentType( "text/plain" ); resp.setCharacterEncoding( "UTF-8" ); //獲取輸出對象 PrintWriter out = resp.getWriter(); //創(chuàng)建一個(gè)基于硬盤的FileItem工廠 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); //設(shè)置向硬盤寫數(shù)據(jù)的緩沖區(qū)大小,這里為4k diskFileItemFactory.setSizeThreshold( 4 * 1024 ); //設(shè)置臨時(shí)目錄 diskFileItemFactory.setRepository( new File(tempFilePath)); //創(chuàng)建一個(gè)文件上傳處理器 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); //設(shè)置文件上傳的大小 servletFileUpload.setSizeMax( 4 * 1024 * 1024 ); //獲取參數(shù) try { //獲取表單參數(shù) List list = servletFileUpload.parseRequest(req); //獲取迭代器 Iterator iterator = list.iterator(); //迭代列表 while (iterator.hasNext()){ //將參數(shù)轉(zhuǎn)型為FileItem類型 FileItem fileItem = (FileItem) iterator.next(); if (fileItem.isFormField()){ System.out.println( "處理表單非文件類型數(shù)據(jù)" ); processFormFiled(fileItem,out); } else { System.out.println( "處理吧表單文件類型數(shù)據(jù)" ); processUploadFile( fileItem,out); } } System.out.println( "關(guān)閉輸出流" ); out.close(); } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } private void processFormFiled(FileItem fileItem,PrintWriter printWriter){ //獲取表單域的名稱 String name = fileItem.getName(); //獲取表單域的值 String value = fileItem.getString(); printWriter.print( "name:" +name+ "value:" +value); } private void processUploadFile(FileItem fileItem,PrintWriter printWriter) throws Exception { //獲取文件名稱 String fileName = fileItem.getName(); int i = fileName.lastIndexOf( "\\" ); fileName = fileName.substring(i+ 1 ,fileName.length()); //獲取文件大小 long fileSize = fileItem.getSize(); System.out.println( "fileName:" +fileName+ "| fileSize:" +fileSize); if ( "" .equals(fileName) && fileSize == 0 ) return ; //將文件寫入指定位置 File upload = new File(filePath+File.separator+fileName); fileItem.write(upload); //輸出結(jié)果 printWriter.print(fileName+ "is saved" ); System.out.println( "處理完畢" ); printWriter.print( "the size of " +fileName+ " is " +fileSize); } } |
3.web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< servlet > < servlet-name >upload</ servlet-name > < servlet-class >com.learn.ServletUpload</ servlet-class > < init-param > < param-name >filePath</ param-name > < param-value >file</ param-value > </ init-param > < init-param > < param-name >tempFilePath</ param-name > < param-value >temp</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >upload</ servlet-name > < url-pattern >/upload</ url-pattern > </ servlet-mapping > |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/wwyx-xi/p/7587741.html