1. 通过getInputStream()取得上传文件。
002 |
* To change this template, choose Tools | Templates |
003 |
* and open the template in the editor. |
005 |
package net.individuals.web.servlet; |
007 |
import java.io.DataInputStream; |
008 |
import java.io.FileOutputStream; |
009 |
import java.io.IOException; |
010 |
import javax.servlet.ServletException; |
011 |
import javax.servlet.annotation.WebServlet; |
012 |
import javax.servlet.http.HttpServlet; |
013 |
import javax.servlet.http.HttpServletRequest; |
014 |
import javax.servlet.http.HttpServletResponse; |
020 |
@WebServlet (name = "UploadServlet" , urlPatterns = { "/UploadServlet" }) |
021 |
public class UploadServlet extends HttpServlet { |
024 |
* Processes requests for both HTTP |
025 |
* <code>GET</code> and |
026 |
* <code>POST</code> methods. |
028 |
* @param request servlet request |
029 |
* @param response servlet response |
030 |
* @throws ServletException if a servlet-specific error occurs |
031 |
* @throws IOException if an I/O error occurs |
033 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
034 |
throws ServletException, IOException { |
035 |
response.setContentType( "text/html;charset=UTF-8" ); |
037 |
byte [] body = readBody(request); |
039 |
String textBody = new String(body, "ISO-8859-1" ); |
041 |
String fileName = getFileName(textBody); |
043 |
Position p = getFilePosition(request, textBody); |
045 |
writeTo(fileName, body, p); |
054 |
public Position( int begin, int end) { |
060 |
private byte [] readBody(HttpServletRequest request) throws IOException { |
062 |
int formDataLength = request.getContentLength(); |
063 |
//取得ServletInputStream输入流对象 |
064 |
DataInputStream dataStream = new DataInputStream(request.getInputStream()); |
065 |
byte body[] = new byte [formDataLength]; |
067 |
while (totalBytes < formDataLength) { |
068 |
int bytes = dataStream.read(body, totalBytes, formDataLength); |
074 |
private Position getFilePosition(HttpServletRequest request, String textBody) throws IOException { |
076 |
String contentType = request.getContentType(); |
077 |
String boundaryText = contentType.substring(contentType.lastIndexOf( "=" ) + 1 , contentType.length()); |
079 |
int pos = textBody.indexOf( "filename=\"" ); |
080 |
pos = textBody.indexOf( "\n" , pos) + 1 ; |
081 |
pos = textBody.indexOf( "\n" , pos) + 1 ; |
082 |
pos = textBody.indexOf( "\n" , pos) + 1 ; |
083 |
int boundaryLoc = textBody.indexOf(boundaryText, pos) - 4 ; |
084 |
int begin = ((textBody.substring( 0 , pos)).getBytes( "ISO-8859-1" )).length; |
085 |
int end = ((textBody.substring( 0 , boundaryLoc)).getBytes( "ISO-8859-1" )).length; |
087 |
return new Position(begin, end); |
090 |
private String getFileName(String requestBody) { |
091 |
String fileName = requestBody.substring(requestBody.indexOf( "filename=\"" ) + 10 ); |
092 |
fileName = fileName.substring( 0 , fileName.indexOf( "\n" )); |
093 |
fileName = fileName.substring(fileName.indexOf( "\n" ) + 1 , fileName.indexOf( "\"" )); |
098 |
private void writeTo(String fileName, byte [] body, Position p) throws IOException { |
099 |
FileOutputStream fileOutputStream = new FileOutputStream( "e:/workspace/" + fileName); |
100 |
fileOutputStream.write(body, p.begin, (p.end - p.begin)); |
101 |
fileOutputStream.flush(); |
102 |
fileOutputStream.close(); |
105 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
108 |
* <code>GET</code> method. |
110 |
* @param request servlet request |
111 |
* @param response servlet response |
112 |
* @throws ServletException if a servlet-specific error occurs |
113 |
* @throws IOException if an I/O error occurs |
116 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
117 |
throws ServletException, IOException { |
118 |
processRequest(request, response); |
123 |
* <code>POST</code> method. |
125 |
* @param request servlet request |
126 |
* @param response servlet response |
127 |
* @throws ServletException if a servlet-specific error occurs |
128 |
* @throws IOException if an I/O error occurs |
131 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
132 |
throws ServletException, IOException { |
133 |
processRequest(request, response); |
137 |
* Returns a short description of the servlet. |
139 |
* @return a String containing servlet description |
142 |
public String getServletInfo() { |
143 |
return "Short description" ; |
2. 通过getPart()、getParts()取得上传文件。
002 |
* To change this template, choose Tools | Templates |
003 |
* and open the template in the editor. |
005 |
package net.individuals.web.servlet; |
007 |
import java.io.FileNotFoundException; |
008 |
import java.io.FileOutputStream; |
009 |
import java.io.IOException; |
010 |
import java.io.InputStream; |
011 |
import java.io.OutputStream; |
012 |
import javax.servlet.ServletException; |
013 |
import javax.servlet.annotation.MultipartConfig; |
014 |
import javax.servlet.annotation.WebServlet; |
015 |
import javax.servlet.http.HttpServlet; |
016 |
import javax.servlet.http.HttpServletRequest; |
017 |
import javax.servlet.http.HttpServletResponse; |
018 |
import javax.servlet.http.Part; |
025 |
@WebServlet (name = "UploadServlet" , urlPatterns = { "/UploadServlet" }) |
026 |
public class UploadServlet extends HttpServlet { |
029 |
* Processes requests for both HTTP |
030 |
* <code>GET</code> and |
031 |
* <code>POST</code> methods. |
033 |
* @param request servlet request |
034 |
* @param response servlet response |
035 |
* @throws ServletException if a servlet-specific error occurs |
036 |
* @throws IOException if an I/O error occurs |
038 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
039 |
throws ServletException, IOException { |
040 |
Part part = request.getPart( "photo" ); |
041 |
String fileName = getFileName(part); |
042 |
writeTo(fileName, part); |
046 |
private String getFileName(Part part) { |
047 |
String header = part.getHeader( "Content-Disposition" ); |
048 |
String fileName = header.substring(header.indexOf( "filename=\"" ) + 10 , header.lastIndexOf( "\"" )); |
054 |
private void writeTo(String fileName, Part part) throws IOException, FileNotFoundException { |
055 |
InputStream in = part.getInputStream(); |
056 |
OutputStream out = new FileOutputStream( "e:/workspace/" + fileName); |
057 |
byte [] buffer = new byte [ 1024 ]; |
059 |
while ((length = in.read(buffer)) != - 1 ) { |
060 |
out.write(buffer, 0 , length); |
067 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
070 |
* <code>GET</code> method. |
072 |
* @param request servlet request |
073 |
* @param response servlet response |
074 |
* @throws ServletException if a servlet-specific error occurs |
075 |
* @throws IOException if an I/O error occurs |
078 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
079 |
throws ServletException, IOException { |
080 |
processRequest(request, response); |
085 |
* <code>POST</code> method. |
087 |
* @param request servlet request |
088 |
* @param response servlet response |
089 |
* @throws ServletException if a servlet-specific error occurs |
090 |
* @throws IOException if an I/O error occurs |
093 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
094 |
throws ServletException, IOException { |
095 |
processRequest(request, response); |
099 |
* Returns a short description of the servlet. |
101 |
* @return a String containing servlet description |
104 |
public String getServletInfo() { |
105 |
return "Short description" ; |
另一种较为简单的方法:
02 |
* To change this template, choose Tools | Templates |
03 |
* and open the template in the editor. |
05 |
package net.individuals.web.servlet; |
07 |
import java.io.IOException; |
08 |
import java.io.PrintWriter; |
09 |
import javax.servlet.ServletException; |
10 |
import javax.servlet.annotation.MultipartConfig; |
11 |
import javax.servlet.annotation.WebServlet; |
12 |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
15 |
import javax.servlet.http.Part; |
18 |
*采用part的wirte(String fileName)上传,浏览器将产生临时TMP文件。 |
21 |
@MultipartConfig (location = "e:/workspace" ) |
22 |
@WebServlet (name = "UploadServlet" , urlPatterns = { "/UploadServlet" }) |
23 |
public class UploadServlet extends HttpServlet { |
26 |
* Processes requests for both HTTP |
27 |
* <code>GET</code> and |
28 |
* <code>POST</code> methods. |
30 |
* @param request servlet request |
31 |
* @param response servlet response |
32 |
* @throws ServletException if a servlet-specific error occurs |
33 |
* @throws IOException if an I/O error occurs |
35 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
36 |
throws ServletException, IOException { |
38 |
request.setCharacterEncoding( "UTF-8" ); |
39 |
Part part = request.getPart( "photo" ); |
40 |
String fileName = getFileName(part); |
45 |
private String getFileName(Part part) { |
46 |
String header = part.getHeader( "Content-Disposition" ); |
47 |
String fileName = header.substring(header.indexOf( "filename=\"" ) + 10 , header.lastIndexOf( "\"" )); |
51 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
54 |
* <code>GET</code> method. |
56 |
* @param request servlet request |
57 |
* @param response servlet response |
58 |
* @throws ServletException if a servlet-specific error occurs |
59 |
* @throws IOException if an I/O error occurs |
62 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
63 |
throws ServletException, IOException { |
64 |
processRequest(request, response); |
69 |
* <code>POST</code> method. |
71 |
* @param request servlet request |
72 |
* @param response servlet response |
73 |
* @throws ServletException if a servlet-specific error occurs |
74 |
* @throws IOException if an I/O error occurs |
77 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
78 |
throws ServletException, IOException { |
79 |
processRequest(request, response); |
83 |
* Returns a short description of the servlet. |
85 |
* @return a String containing servlet description |
88 |
public String getServletInfo() { |
89 |
return "Short description" ; |
使用getParts()上传多个文件:
02 |
To change this template, choose Tools | Templates |
03 |
and open the template in the editor. |
09 |
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > |
13 |
< form action = "UploadServlet3" method = "POST" enctype = "multipart/form-data" > |
16 |
< td >< label for = "file1" >文件1:</ label ></ td > |
17 |
< td >< input type = "file" id = "file1" name = "file" ></ td > |
20 |
< td >< label for = "file2" >文件2:</ label ></ td > |
21 |
< td >< input type = "file" id = "file2" name = "file" ></ td > |
24 |
< td >< label for = "file3" >文件3:</ label ></ td > |
25 |
< td >< input type = "file" id = "file3" name = "file" ></ td > |
28 |
< td colspan = "2" >< input type = "submit" value = "上传" name = "upload" ></ td > |
对应Servlet代码:
02 |
* To change this template, choose Tools | Templates |
03 |
* and open the template in the editor. |
05 |
package net.individuals.web.servlet; |
07 |
import java.io.IOException; |
08 |
import java.io.PrintWriter; |
09 |
import javax.servlet.ServletException; |
10 |
import javax.servlet.annotation.MultipartConfig; |
11 |
import javax.servlet.annotation.WebServlet; |
12 |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
15 |
import javax.servlet.http.Part; |
21 |
@MultipartConfig (location = "e:/workspace" ) |
22 |
@WebServlet (name = "UploadServlet" , urlPatterns = { "/UploadServlet" }) |
23 |
public class UploadServlet extends HttpServlet { |
26 |
* Processes requests for both HTTP |
27 |
* <code>GET</code> and |
28 |
* <code>POST</code> methods. |
30 |
* @param request servlet request |
31 |
* @param response servlet response |
32 |
* @throws ServletException if a servlet-specific error occurs |
33 |
* @throws IOException if an I/O error occurs |
35 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
36 |
throws ServletException, IOException { |
37 |
request.setCharacterEncoding( "utf-8" ); |
38 |
//迭代Collection中所有Part对象 |
39 |
for (Part part : request.getParts()) { |
41 |
if (part.getName().startsWith( "file" )) { |
42 |
String fileName = getFileName(part); |
48 |
private String getFileName(Part part) { |
49 |
String header = part.getHeader( "Content-Disposition" ); |
50 |
String fileName = header.substring(header.indexOf( "filename=\"" ) + 10 , header.lastIndexOf( "\"" )); |
51 |
header.lastIndexOf( "\"" ); |
55 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
58 |
* <code>GET</code> method. |
60 |
* @param request servlet request |
61 |
* @param response servlet response |
62 |
* @throws ServletException if a servlet-specific error occurs |
63 |
* @throws IOException if an I/O error occurs |
66 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
67 |
throws ServletException, IOException { |
68 |
processRequest(request, response); |
73 |
* <code>POST</code> method. |
75 |
* @param request servlet request |
76 |
* @param response servlet response |
77 |
* @throws ServletException if a servlet-specific error occurs |
78 |
* @throws IOException if an I/O error occurs |
81 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
82 |
throws ServletException, IOException { |
83 |
processRequest(request, response); |
87 |
* Returns a short description of the servlet. |
89 |
* @return a String containing servlet description |
92 |
public String getServletInfo() { |
93 |
return "Short description" ; |
- Servlet实现文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip : 点击打开链接 2) commons- ...
- java常见3种文件上传速度对比和文件上传方法详细代码
在java里面文件上传的方式很多,最简单的依然是FileInputStream.FileOutputStream了,在这里我列举3种常见的文件上传方法代码,并比较他们的上传速度(由于代码是在本地测试, ...
- Servlet实现文件上传,可多文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...
- 配置servlet支持文件上传
Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...
- jsp+servlet实现文件上传下载
相关素材下载 01.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- java commons-fileupload servlet 多文件上传
commons-fileupload servlet 多文件上传 需要引入的 jar 包. commons-fileupload-1.3.2.jar commons-io-2.2.jar 工程路劲:查 ...
- Servlet之文件上传
上传表单中的注意事项: 表单 method 属性应该设置为 POST 方法,不能使用 GET 方法 表单 enctype 属性应该设置为multipart/form-data 下面的实例是借助于com ...
- 使用FileUpload实现Servlet的文件上传
简介 FileUpload 是 Apache commons下面的一个子项目,用来实现Java环境下的文件上传功能. FileUpload链接 FileUpload 是基于Apache的Commons ...
- servlet实现文件上传,预览,下载和删除
一.准备工作 1.1 文件上传插件:uploadify: 1.2 文件上传所需jar包:commons-fileupload-1.3.1.jar和commons-io-2.2.jar 1.3 将数 ...
随机推荐
- 论文笔记(3)-Extracting and Composing Robust Features with Denoising Autoencoders
这篇文章是Bengio研究的在传统的autoencoder基础上增加了噪声参数,也就是说在输入X的时候,并不直接用X的数据,而是按照一定的概率来清空输入为0.paper中的名词为corrupted.这 ...
- List<T>用法
所属命名空间:System.Collections.Generic public class List<T> : IList<T>, ICollection<T>, ...
- JavaScript中Ajax的使用
AJAX全称为“Asynchronous javascript and XML”(异步javascript和XML),是指一种创建交互式网页应用的网页开发技术.通过在后台与服务器进行少量数据交换,AJ ...
- VSTS 更名为 Azure DevOps
微软正式对外宣布Azure DevOps,其实就是原来的VSTS,我们来看一下Azure DevOps的介绍: 今天我们宣布Azure DevOps.与世界各地的客户和开发人员合作,很明显,DevOp ...
- Dalsa线扫相机配置-一台工控机同时连接多个GigE相机
如图,我强悍的工控机,有六个网口. 实际用的时候连了多台相机,为了偷懒我就把六个网口的地址分别设为192.168.0.1~192.168.0.6,以为相机的IP只要设在192.168.0这个网段然后随 ...
- c# 输入多个数字,当输入不是数字时显示出刚输入的所有数并按降序
输入多个数字,当输入不是数字时显示出刚输入的所有数并按降序 class Program { static void Main(string[] args) { //定于一个集合 List<int ...
- Word发表blog格式模板
一级标题(黑体,二号,加粗) 二级标题(黑体,三号,加粗) 正文(宋体+Times New Roman,小四) 注意事项: 序号列表"不连续"时,不得使用自动序号 连续(word连 ...
- super函数的用法
1.创建一个类. # .创建一个类 class Bird: def __init__(self): self.hungry =True def eat(self): if self.hungry: p ...
- ovs 源mac, 目的src 互换
push:NXM_OF_ETH_SRC[],push:NXM_OF_ETH_DST[],pop:NXM_OF_ETH_SRC[],pop:NXM_OF_ETH_DST[] 1:把src mac推到栈顶 ...
- Android - "已安装了存在签名冲突的同名数据包",解决方法!
错误提示:已安装了存在签名冲突的同名数据包. 解决方法:打开Android Studio,打开logcat,用usb线连接你出错的手机,识别出手机之后,在你的项目后面,点击“run”按钮,随后AS会提 ...