java上传处理-Servlet 批量上传大文件
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
新建类UploadServlet
Java代码
1.package org.action;
2.
3.import java.io.File;
4.import java.io.IOException;
5.import java.util.Iterator;
6.import java.util.List;
7.
8.import javax.servlet.ServletException;
9.import javax.servlet.http.HttpServlet;
10.import javax.servlet.http.HttpServletRequest;
11.import javax.servlet.http.HttpServletResponse;
12.
13.import mons.fileupload.FileItem;
14.import mons.fileupload.disk.DiskFileItemFactory;
15.import mons.fileupload.servlet.ServletFileUpload;
16.
17.@SuppressWarnings("unchecked")
18.public class UploadServlet extends HttpServlet{
19.
20.private static final long serialVersionUID = 1L ;
21.
22.@Override
23.protected void doGet(HttpServletRequest req, HttpServletResponse resp)
24.throws ServletException, IOException {
25. doPost(req, resp) ;
26. }
27.
28.@Override
29.protected void doPost(HttpServletRequest request, HttpServletResponse re
sponse )
30.throws ServletException, IOException {
31. DiskFileItemFactory factory = new DiskFileItemFactory() ;
32. factory.setSizeThreshold( 8192 ) ;
33. String tempPath = request.getSession().getServletContext().getRealPa
th( "/")
34. + "/upload" ;
35. File file = new File( tempPath ) ;
36.if(!file.isDirectory()){
Java代码
56. e.printStackTrace() ;
57. }
58. }
59.
60.}
在web.xml 中配置:
Html代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
Html代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
页面代码:
Java代码
1.<%@ page language="java"import="java.util.*" pageEncoding="ISO-8859-1"%>
2.<%
3.String path = request.getContextPath();
4.String basePath = request.getScheme()+"://"+request.getServerName()+":"+requ
est.getServerPort()+path+"/";
5.%>
6.
7.
Java代码
依赖的jar包,commons-fileupload-1.1.jar 和commons-io-1.1.jar