本文实例为大家分享了servlet上传文件的具体代码,供大家参考,具体内容如下
1.servlet上传文件
servlet上传文件就是将客户端的文件上传到服务器端。
向服务器发送数据时,客户端发送的http请求正文采用“multipart/form-data”数据类型,他表示复杂的多个子部分的复合表单。
为了简化“multipart/form-data”数据的处理过程。可以使用Apache组织提供是的两个开源包来来实现上传。
fileupload软件包(commons-fileupload-1.2.1.jar),负责文件上传的软件包。
io软件包(commons-io-1.4.jar)负责输入输出的软件包。
2.servlet上传文件相关类
2.1fileupload软件包把请求正文包含的复合表单的每个子部分看做FileItem对象。FileItem对象分为两种类型。
(1)formFiled:普通表单域类型,如表单中的文本和按钮等。
(2)非formFiled:上传文件类型,表单中的文件域就是这种类型。
2.2FileItemFactory接口和FileItem接口
FileItemFactory 是创建FileItem 对象的工厂。
DiskFileItemFactory 实现了FileItemFactory接口,DiskFileItemFactory用于创建DiskFileItem对象。
DiskFileItem对象用于把客户端上传的文件保存在客户端。
2.3ServletFileUpload 类
ServletFileUpload为文件上传处理器。和DiskFileItemFactory对象关联。
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);
//获取文件路径真实值
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);
//设置返回内容为纯文字内容
resp.setContentType( "text/plain" );
resp.setCharacterEncoding( "UTF-8" );
//获取输出对象
PrintWriter out = resp.getWriter();
//创建一个基于硬盘的FileItem工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
//设置向硬盘写数据的缓冲区大小,这里为4k
diskFileItemFactory.setSizeThreshold( 4 * 1024 );
//设置临时目录
diskFileItemFactory.setRepository( new File(tempFilePath));
//创建一个文件上传处理器
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
//设置文件上传的大小
servletFileUpload.setSizeMax( 4 * 1024 * 1024 );
//获取参数
try {
//获取表单参数
List list = servletFileUpload.parseRequest(req);
//获取迭代器
Iterator iterator = list.iterator();
//迭代列表
while (iterator.hasNext()){
//将参数转型为FileItem类型
FileItem fileItem = (FileItem) iterator.next();
if (fileItem.isFormField()){
System.out.println( "处理表单非文件类型数据" );
processFormFiled(fileItem,out);
} else {
System.out.println( "处理吧表单文件类型数据" );
processUploadFile( fileItem,out);
}
}
System.out.println( "关闭输出流" );
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);
//输出结果
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 >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/wwyx-xi/p/7587741.html