servlet上传文件的实现

简介:

下载两样东西 

都来自于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

相关文章
|
10月前
|
JavaScript 前端开发 Java
【JavaEE】使Cookie与Session失效-Servlet上传文件操作-优化表白墙
虽然Cookie和Session都是暂时存在的,不久就会被删掉,但是我们要退出登录的时候,就不能等待其自然消除了~
49 0
|
10月前
|
存储 前端开发 Java
【JavaEE】使Cookie与Session失效-Servlet上传文件操作-优化表白墙
虽然Cookie和Session都是暂时存在的,不久就会被删掉,但是我们要退出登录的时候,就不能等待其自然消除了~
78 0
|
Windows 应用服务中间件
Servlet 上传文件
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.
723 0
|
Java
JAVA SERVLET上传文件的样码
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.
736 0
|
Java Android开发 容器
|
存储 安全 Apache
|
Java Apache 数据库连接
【java】利用servlet解析报文,上传文件
由于工作需要,需要做一个excel导入的功能,这就需要上传excel文件到服务器,服务器做逻辑判断此文件是否是excel文件,接着利用poi的api就可以将内容转化为利用jdbc插入到数据库,达到批量数据导入的功能。
1020 0
纯Servlet上传文件
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class UploadServlet extends HttpServlet {    ...
460 0
|
26天前
|
Java
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
20 1
|
26天前
|
Java
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
19 0