且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

servlet上传文件的实现

更新时间:2022-10-04 20:31:10

下载两样东西 

都来自于apache的commons 项目 

commons-fileupload-1.3-bin.zip:http://commons.apache.org/proper/commons-fileupload/

上面那个依赖于 commons-io-2.4-bin.zip

commons-io-2.4-bin.zip: http://commons.apache.org/proper/commons-io/

将两个文件解压后 分别找到找到其中lib下的commons-io-2.4.jar 和 commons-fileupload-1.3.jar  放置在 WEB-INF\lib 中


编写上传时候需要的 jsp代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fileUpload.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>Insert title here</title>
</head>
<body>
<form action="UploadServlet" method="post"  enctype="multipart/form-data">
username:<input type="text" name="username"><br/>
file: <input type="file" name="file"><br/>
file2: <input type="file" name="file2"><br/>
<input type="submit" value="submit"><br/>
</form>
</body>
</html>
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
UploadServlet.java
用于处理上传过来的文件的servlet
===================================
import java.io.File;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = req.getRealPath("/upload");
factory.setRepository(new File(path));
factory.setSizeThreshold(1024 1024);
ServletFileUpload upload = new ServletFileUpload(factory);
try{
List<FileItem> list = (List<FileItem>)upload.parseRequest(req);
for(FileItem item : list){
String name = item.getFieldName();
if(item.isFormField()){
String value = item.getString();
System.out.println(name+" = "+value);
req.setAttribute(name, value);
}
else{
String value = item.getName();
System.out.println(value);
int start = value.lastIndexOf("\\");
String fileName = value.substring(start+1);
System.out.println(name+" = "+value);
req.setAttribute(name, fileName);
item.write(new File(path, fileName));
}
}
}catch(Exception ex){
ex.printStackTrace();
}
req.getRequestDispatcher("fileUploadResult.jsp").forward(req, resp);
}
}


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
fileUploadResult.jsp
上传完后提示 展示的
=================================
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*, java.io.*"
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>Insert title here</title>
</head>
<body>
<%-- <%
InputStream is = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
while(null != (buffer=br.readLine())){
out.print(buffer+"<br>");
}
br.close();
is.close();
%> --%>
username: ${requestScope.username }<br />
file: ${requestScope.file }<br />
file: ${requestScope.file2 }<br />
</body>
</html>

=======================================================================

注意:整个程序就这么写好了

需要在 WebContent 下创建一个upload文件夹 进行上传文件的保存

但是如果直接在eclipse jee 当中的tomcat运行的话 保存 到的目录会在别的地址当中 应该是 跟运行环境有关

需要打成war包部署到tomcat当中运行才能正确的上传到 指定的位置



本文转自    拖鞋崽      51CTO博客,原文链接:http://blog.51cto.com/1992mrwang/1242815