版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/twilight_karl/article/details/69917866
更改form的enctype属性
enctype属性默认是application/x-www-form-urlencoded,发送文件时需要更改为multipart/form-data
<form action=”/File/Photo” method=”post” enctype=”multipart/form-data”>
发送一个图片时的http请求如下:
POST /File/Photo HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
X-HttpWatch-RID: 90747-10177
Referer: http://localhost:8080/File/photo.html
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: multipart/form-data; boundary=---------------------------7e179f4038a
Accept-Encoding: gzip, deflate
Host: localhost:8080
Content-Length: 566814
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7e179f4038a
Content-Disposition: form-data; name="file"; filename="D:\鍥剧墖\Camera Roll\b.jpg"
Content-Type: image/jpeg
表单如下:
<form action="/File/Photo_Stream" method="post" enctype="multipart/form-data">
<input type="file" name='file' /><br/>
<input type="submit" value="提交" />
</form>
使用Part对象接受文件
Part对象也可以获取http头部信息
part.getHeader(“name”) :获取头部信息中name对应的值
part.write(fileName) : 将part对象写入到文件中,文件的路径在 @MultipartConfig中配置
getHeader(“Content-Disposition”)的结果是:
form-data; name=”file”; filename=”D:\鍥剧墖\Camera Roll\b.jpg”
因此可以根据Content-Disposition属性的值获取文件的名称
getPart(name)
Part对象不需要通过构造函数创建,可以通过req.getPart()方法返回对应的文件对象
Part part = req.getPart("file");
// 获取文件名
String header = part.getHeader("Content-Disposition");
String fileName = header.substring(header.indexOf("filename=\"")+10,header.length()-1);
fileName = fileName.substring(3);
// 写入到文件
part.write(fileName);
getParts()
getParts() 返回所有的文件对象,返回类型是Collection集合。需要遍历集合才能处理所有文件
Collection<Part> part = req.getParts();
for(Part temp :part){
String header = temp.getHeader("Content-Disposition");
// 获取文件名
String fileName = header.substring(header.indexOf("filename=\"")+10,header.length()-1);
output.println("<br/>Content-Disposition:"+header);
output.println("<br/>fileName:"+fileName);
fileName = fileName.substring(3);
// 写入到文件
temp.write(fileName);
}
@MultipartConfig
Tomcat中必须标注@MultipartConfig才能使用 Part
@MultipartConfig可以设置上传文件的相关信息:
fileSizeThreshold : 上传文件大小的门槛 location : 配置文件的保存位置 maxRequestSize : 设置请求个数,默认值为-1L,表示不限个数// 表示上传的文件在write()时保存到E盘
@MultipartConfig(location="E:/")
也可以在web.xml中配置@MultipartConfig
<servlet>
<servlet-name>Photo</servlet-name>
<servlet-class>Files.Photo</servlet-class>
<multipart-config>
<location>E:/</location>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>Photo</servlet-name>
<url-pattern>/Photo</url-pattern>
</servlet-mapping>
使用Part的InputStream
可以使用Part的getInputStream()方法返回一个输入流,再使用传统的方式将输入流输出到文件
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
// 获取part对象
Part part = req.getPart("file");
// 获取输入流
InputStream data = part.getInputStream();
byte [] temp = new byte [1024];
FileOutputStream output = new FileOutputStream(new File("E:/s.jpg"));
int len = 0 ;
while(-1 != (len = data.read(temp))){
output.write(temp, 0, len);
}
output.flush();
output.close();
}
参考:
JonnyWei的专栏