参考转载:
https://www.cnblogs.com/EasonJim/p/6554669.html
https://my.oschina.net/Barudisshu/blog/150026
原理:
利用getPart()、getParts()读取每个boundary部分的内容,然后通过header获取文件名,通过getInputStream()获取文件内容,然后读取到服务器。
读取到的内容形式一般如下,使用--boundary分割每个表单项。
————————————————
版权声明:本文为CSDN博主「yzy85」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yzy85/article/details/104188062
代码:
upload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>upload</title>
</head>
<body>
<p>选择要上传的文件</p>
<form action="<%=request.getContextPath()%>/UploadServlet" method="post" enctype="multipart/form-data">
<input type="text" name="girl" value="susan">
<input type="file" name="boy" size="38">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
UploadServlet.java
在Servlet上必须标注特性标记头@MultipartConfig或者在web.xml的servlet节点中配置<multipart-config></multipart-config>,以表示是multipart/form-data类型的MIME。
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.processRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Part part = request.getPart("boy");
String header = part.getHeader("Content-Disposition");
String fileName = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\""));
InputStream in = part.getInputStream();
String filePath = this.getServletContext().getRealPath("/") + "/Upload/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/";
File dir = new File(filePath);
dir.mkdirs();
OutputStream out = new FileOutputStream(filePath + fileName);
byte[] buffer = new byte[1024];
int length = -1;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
in.close();
out.close();
response.getWriter().println("upload success");
// Part part1 = request.getPart("girl");
// in = part1.getInputStream();
// length = 0;
// int off = 0;
// buffer = new byte[1024];
// length = in.read(buffer, off, 100);
//
// while (length != -1) {
// off = off + length;
// length = in.read(buffer, off, 100);
// }
// String value = new String(buffer,0,off);
//读取非文件内容,将文件流转换为字节流,然后转成字节数组
Part part1 = request.getPart("girl");
in = part1.getInputStream();
ByteArrayOutputStream outByte = new ByteArrayOutputStream();
buffer=new byte[1024];
length = 0;
while ((length = in.read(buffer, 0, 1024)) != -1) {
outByte.write(buffer,0,length);
}
String value = new String(outByte.toByteArray());
response.getWriter().println("upload success");
response.getWriter().println("girls=" + value);
}
}