标签:必须 web项目 表单 -name location 一个 create https position
1.idea创建web项目
2.项目目录结构
UploadServlet类:
package com.atguigu.servlet; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.ServletException; 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); //设置编码 servletFileUpload.setHeaderEncoding("UTF-8"); try { // 解析上传的数据,得到每一个表单项FileItem List<FileItem> list = servletFileUpload.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(); } } } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>UploadServlet</servlet-name> <servlet-class>com.atguigu.servlet.UploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/uploadServlet</url-pattern> </servlet-mapping> </web-app>
upload.jsp:
<%-- Created by IntelliJ IDEA. User: Administrator Time: 16:21 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="http://localhost:8080/upload01/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>
访问http://localhost:8080/upload01/upload.jsp点击上传结果如下:
1.下载案例
项目目录结构:
Download类:
package com.atguigu.servlet; import org.apache.commons.io.IOUtils; import sun.misc.BASE64Encoder; 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 = "2.jpg"; //2、读取要下载的文件内容 (通过ServletContext对象可以读取) ServletContext servletContext = getServletContext(); // 获取要下载的文件类型 String mimeType = servletContext.getMimeType("/file/" + downloadFileName); System.out.println("下载的文件类型:" + mimeType); //4、在回传前,通过响应头告诉客户端返回的数据类型 resp.setContentType(mimeType); //5、还要告诉客户端收到的数据是用于下载使用(还是使用响应头) // Content-Disposition响应头,表示收到的数据怎么处理 // attachment表示附件,表示下载使用 // filename= 表示指定下载的文件名 // url编码是把汉字转换成为%xx%xx的格式 if (req.getHeader("User-Agent").contains("Firefox")) { // 如果是火狐浏览器使用Base64编码 resp.setHeader("Content-Disposition", "attachment; filename==?UTF-8?B?" + new BASE64Encoder().encode("中国.jpg".getBytes("UTF-8")) + "?="); } else { // 如果不是火狐,是IE或谷歌,使用URL编码操作 resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("中国.jpg", "UTF-8")); } /** * /斜杠被服务器解析表示地址为http://ip:prot/工程名/ 映射 到代码的Web目录 */ InputStream resourceAsStream = servletContext.getResourceAsStream("/file/" + downloadFileName); // 获取响应的输出流 OutputStream outputStream = resp.getOutputStream(); //3、把下载的文件内容回传给客户端 // 读取输入流中全部的数据,复制给输出流,输出给客户端 IOUtils.copy(resourceAsStream, outputStream); } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>Download</servlet-name> <servlet-class>com.atguigu.servlet.Download</servlet-class> </servlet> <servlet-mapping> <servlet-name>Download</servlet-name> <url-pattern>/download</url-pattern> </servlet-mapping> </web-app>
标签:必须 web项目 表单 -name location 一个 create https position
原文地址:https://www.cnblogs.com/wuwuyong/p/14855243.html