首页 > 代码库 > Servlet3.0上传文件

Servlet3.0上传文件

 1 package com.itheima.upload;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.MultipartConfig;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.Part;
16 
17 import com.itheima.utils.UUIDUtils;
18 import com.itheima.utils.UploadUtils;
19 /**
20  * 文件上传的Servlet
21  * 表单项:分为两类 一类是文件表单项  一类是非文件表单项(普通表单项,不必设置enctype="mutipart/form-data")
22  */
23 @MultipartConfig
24 @WebServlet("/UploadServlet")
25 public class UploadServlet extends HttpServlet {
26     private static final long serialVersionUID = 1L;
27     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28         //接受普通数据 非文件表单项 即普通表单项 不必设置enctype="multipart/form-data"
29         request.setCharacterEncoding("UTF-8");
30         String filedesc = request.getParameter("filedesc");
31         System.out.println("文件描述:"+filedesc);
32         //接受文件 文件表单项 必须满足三要素 post form enctype="multipart/form-data"
33         //通过表单file控件(<input type="file" name="upload">)的名字属性直接获取Part对象
34         Part part = request.getPart("upload");
35         //获得文件大小
36         long size = part.getSize();
37         System.out.println("文件的大小为:"+size);
38         String name = part.getName();
39         System.out.println("文件上传表单的name属性的值为:"+name);
40         //获取头信息
41         String header = part.getHeader("Content-Disposition");
42         System.out.println(header);
43         //获取文件名字 \"为转义 
44         int lastIndexOf = header.lastIndexOf("filename=\"");
45         String filename = header.substring(lastIndexOf+10, header.length()-1);
46         System.out.println("文件名称:"+filename);
47         //获得文件内容
48         InputStream ips = part.getInputStream();
49         //获取要上传到的文件夹路径
50         String realPath = this.getServletContext().getRealPath("/upload");
51         System.out.println(realPath);
52         //获得唯一文件名
53         String uniname = UUIDUtils.getUUIDFileName(filename);
54         //获得新的上传路径
55         String fenglipath = realPath + UploadUtils.getPath(uniname);
56         System.out.println(fenglipath);
57         //创建上传路径的file对象
58         File file = new File(fenglipath);
59         //如果上传路径不存在,就创建
60         if(!file.exists()){
61             file.mkdirs();
62         }
63         //创建字节输出流,参数为"路径+文件名"
64         OutputStream ops = new FileOutputStream(fenglipath+"/"+uniname);
65         //io流读写
66         byte [] bytes = new byte [1024];
67         int len;
68         while((len=ips.read(bytes))!=-1){
69             ops.write(bytes, 0, len);
70         }
71         //关流
72         ips.close();
73         ops.close();
74         
75         
76     }
77     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
78         doGet(request, response);
79     }
80 
81 }

 

Servlet3.0上传文件