Servlet实现文件的上传与下载
前言:
文件的上传和下载在日常开发中很是常见,那么这一功能是如何实现的呢,下面我给大家介绍一下
实现条件:
1、需要一个form标签,method为post请求
2、form的encType属性值为multipart/form-data
3、input标签的type=file
4.需要的jar包()
工程目录:
具体实现:
UploadServlet.java(上传)
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; 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 { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.判断上传的数据是否是多段数据(只有事多段的数据才是文件上传的) if(FileUpload.isMultipartContent(req)){ //创建FileItemFactory工厂实现类 FileItemFactory fileItemFactory=new DiskFileItemFactory(); //创建用于解析上传数据的工具类FileUpload FileUpload fileUpload=new FileUpload(fileItemFactory); try { List<FileItem> list=fileUpload.parseRequest(req); //循环判断每一个表单项是普通类型还是上传的文件 for(FileItem fileItem:list){ if(fileItem.isFormField()){ //普通表单项 System.out.println("表单项的name属性值:"+fileItem.getFieldName()); //参数UTF-8解决乱码问题 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:\\"+fileItem.getName())); } } } catch (Exception e) { e.printStackTrace(); } } } }
download.java(下载)
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; import java.net.URLEncoder; public class download extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取要下载的文件名 String downloadfilename="1.jpg"; //2.读取下载的文件内容(通过ServletContext对象可以读取) ServletContext servletContext=getServletContext(); //获取要下载的文件类型 String mimetype=servletContext.getMimeType("/file/"+downloadfilename); //4.回传前告诉客户端回传的类型 resp.setContentType("mimetype"); //5.告诉客户端收到的数据是用来下载的(响应头) //Content-Disposition 响应头表示收到的数据怎么处理 //attachment表示附件,表示下载 resp.setHeader("Content-Disposition","attachment;filename="+downloadfilename); //url编码解决下载文件名乱码问题(谷歌和IE) // resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("中国.jpg","UTF-8")); InputStream resourceAsStream= servletContext.getResourceAsStream("/file/"+downloadfilename); //获取响应的输出流 OutputStream outputStream=resp.getOutputStream(); //3,把下载的文件传回给客户端 //读取输入流的数据复制给输出流,输出给客户端 IOUtils.copy(resourceAsStream,outputStream); } }
前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="http://localhost:8080/upandload_war_exploded/uploadServlet" method="post" enctype="multipart/form-data" > 用户名:<input type="text" name="username"><br> 头像:<input type="file" name="photo"><br> <input type="submit" value="上传"> </form> </body> </html>
成果展示:
文件的上传
服务器接受上传的文件
下载文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)