1.文件上传的介绍
JSP 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器。上传的文件可以是文本文件或图像文件或任何文档。
步骤: (1)要有一个form标签,method=post请求 (2)form标签的encType属性值必须为 multipart/form-data 值 (3)在form标签中使用input type=file添加上传的文件 (4)编写服务器代码接受,处理上传的数据
upload.jsp
<body>
<form action="http://localhost:8080/file/uploadServlet" method="post" enctype="multipart/form-data">
用户名:<label><input type="text" name="username"/></label><br/><br/>
头像:<label><input type="file" name="photo"/></label><br/><br/>
<input type="submit" value="上传"/>
</form>
</body>
enctype="multipart/form-data"表示提交的数据,以多段(每一个表单项一个数据段)的形式进行拼接,然后以二进制的形式发送给服务器。
UploadServlet.java
package com.sdnu.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("文件上传成功");
}
}
2.上传的http协议内容介绍
修改UploadServlet.java
public class UploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletInputStream inputStream = req.getInputStream();
byte[] buffer = new byte[1024];
int read = inputStream.read(buffer);
System.out.println(new String(buffer, 0 , read));
}
}
3.使用fileUpload解析上传的数据
UploadServlet.java
package com.sdnu.servlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class UploadServlet extends HttpServlet {
/**
* 处理文件上传
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.先判断上传的数据是否是多段,只有是多段的,才是文件上传。
if(ServletFileUpload.isMultipartContent(req)){
//创建FileItemFactory工厂实现类
FileItemFactory fileItemFactory = new DiskFileItemFactory();
//用于解析上传数据的工具类ServletFileUpload类
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
try {
//解析上传的数据,得到一个表单项FileItem
List<FileItem> list = servletFileUpload.parseRequest(req);
for(FileItem fileItem : list){
if(fileItem.isFormField()){
System.out.println("表单项的name属性:" + fileItem.getFieldName());
System.out.println("表单项的value属性值:" + fileItem.getString("UTF-8"));
}
else {
System.out.println("表单项的name属性:" + fileItem.getFieldName());
System.out.println("上传的文件名" + fileItem.getName());
fileItem.write(new File("D:\\test\\" + fileItem.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
4.文件的下载
package com.sdnu.servlet;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DownLoad extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.读取要下载的文件名
String downloadFileName = "images.jpg";
//2.读取要下载的文件内容(通过ServletContext对象可以读取)
ServletContext servletContext = getServletContext();
//3.获取要下载的文件类型
String mimeType = servletContext.getMimeType("/file/" + downloadFileName);
System.out.println("下载的文件类型" + mimeType);
//4.在回传前,通过响应头告诉客户端返回的数据类型
resp.setContentType(mimeType);
//5.告诉客户端收到的数据是用于下载使用(还是使用响应头)
resp.setHeader("Content-Disposition", "attachment; filename=" + downloadFileName);
InputStream resourceAsStream = servletContext.getResourceAsStream("/file/" + downloadFileName);
//6.获取响应的输出流
OutputStream outputStream = resp.getOutputStream();
//7.读取输入流中全部的数据,复制给输出流,输出给客户端
IOUtils.copy(resourceAsStream, outputStream);
//8.把下载的文件内容回传给客户端
}
}
中文下载乱码解决方案:
resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("图片中文名称.jpg", "UTF-8")
作者:Beyong 出处:Beyong博客 github地址:https://github.com/beyong2019
本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。
|