💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 文件的上传与下载 upload.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="upload.do" method="post" enctype="multipart/form-data"> <input type="text" name="username"> <input type="file" name="file1"> <input type="submit"> </form> <br /> <form action="upload.do" method="post" enctype="multipart/form-data"> <input type="file" name="file2"> <input type="file" name="file3"> <input type="submit"> </form> </body> </html> ~~~ uploadServlet ~~~ package com.neusoft.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Collection; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/upload.do") @MultipartConfig public class UploadServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // 存储路径 String savePath = request.getServletContext().getRealPath("/WEB-INF/uploadFile"); File file = new File(savePath); if(!file.exists()) { file.mkdir(); } // 获取上传的文件集合 Collection<Part> parts = request.getParts(); // 上传单个文件 if (parts.size() == 1) { // Servlet3.0将multipart/form-data的POST请求封装成Part,通过Part对上传的文件进行操作。 // Part part = parts[0];//从上传的文件集合中获取Part对象 Part part = request.getPart("file1");// 通过表单file控件(<input type="file" name="file">)的名字直接获取Part对象 // Servlet3没有提供直接获取文件名的方法,需要从请求头中解析出来 // 获取请求头,请求头的格式:form-data; name="file"; filename="snmp4j--api.zip" String header = part.getHeader("content-disposition"); // 获取文件名 String fileName = getFileName(header); // 把文件写到指定路径 part.write(savePath + File.separator + fileName); } else { // 一次性上传多个文件 for (Part part : parts) {// 循环处理上传的文件 // 获取请求头,请求头的格式:form-data; name="file"; filename="snmp4j--api.zip" String header = part.getHeader("content-disposition"); // 获取文件名 String fileName = getFileName(header); // 把文件写到指定路径 part.write(savePath + File.separator + fileName); } } PrintWriter out = response.getWriter(); out.println("上传成功"); out.flush(); out.close(); } public String getFileName(String header) { /** * String[] tempArr1 = header.split(";");代码执行完之后,在不同的浏览器下,tempArr1数组里面的内容稍有区别 * 火狐或者google浏览器下:tempArr1={form-data,name="file",filename="snmp4j--api.zip"} * IE浏览器下:tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"} */ String[] tempArr1 = header.split(";"); /** * 火狐或者google浏览器下:tempArr2={filename,"snmp4j--api.zip"} * IE浏览器下:tempArr2={filename,"E:\snmp4j--api.zip"} */ String[] tempArr2 = tempArr1[2].split("="); // 获取文件名,兼容各种浏览器的写法 String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", ""); return fileName; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ~~~ download.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="downloadServlet">文件下载</a> </body> </html> ~~~ DownloadServlet ~~~ package com.neuedu.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/downloadServlet") public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/x-msdownload"); String fileName = "3.jpg"; response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); OutputStream out = response.getOutputStream(); String pptFileName = "D:\\yznworkspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\fileupload\\upload\\0e116136-53a8-4356-b7a7-68263d1e10493.jpg"; InputStream in = new FileInputStream(pptFileName); byte [] buffer = new byte[1024]; int len = 0; while((len = in.read(buffer)) != -1){ out.write(buffer, 0, len); } in.close(); } } ~~~