javax.servlet.http.Part 文件上传

时间:2022-08-28 20:23:30

 

 

编辑jsp页面:

 1 <html>
 2   <head>
 3     <base href="<%=basePath%>">
 4     
 5     <title>My JSP 'index.jsp' starting page</title>
 6     <meta http-equiv="pragma" content="no-cache">
 7     <meta http-equiv="cache-control" content="no-cache">
 8     <meta http-equiv="expires" content="0">    
 9     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
10     <meta http-equiv="description" content="This is my page">
11     <!--
12     <link rel="stylesheet" type="text/css" href="styles.css">
13     -->
14   </head>
15   
16   <body>
17       <div class="container">
18               <!-- 文件上传时必须设置 entype 为 multipart/form-data  不对字符进行编码 -->
19             <form action="UploadServlet" method="post" enctype="multipart/form-data">
20                 <table>
21                     <tr>
22                           <!-- 设置 for ="file" 在鼠标点击标签时会弹出文件选取框 -->
23                         <td><label for="file">上传文件:</label></td>
24                         <td><input type="file" id="file" name="picture" value=""/></td>
25                     </tr>
26                     <tr>
27                         <!-- 设置占据两个单元格 -->
28                         <td  colspan="2" ><input type="submit" value="提交"/></td>
29                     </tr>
30                 </table>
31             </form>
32         </div> 
33     
34   </body>
35 </html>

 

编辑servlet:

 1 import java.io.IOException;
 2 import java.util.UUID;
 3 
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.MultipartConfig;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.Part;
11 
12 @WebServlet("/UploadServlet")
13 @MultipartConfig
14 public class UploadServlet extends HttpServlet{
15 
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
18             throws ServletException, IOException {
19         super.doPost(req, resp);
20     }
21     @Override
22     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
23             throws ServletException, IOException {
24         //获取文件部件 part
25         Part part =  req.getPart("upload");
26         //获取服务器的路径
27         String root = req.getServletContext().getRealPath("/upload");
28         System.out.println("服务器路径:"+root);
29 /**
30  *     可以选择 使用getSubmittedFikeName 获取文件名 或从文件头部信息中截取文件名    
31  */
32         /**
33          * 获取文件名
34          */
35         String name= part.getSubmittedFileName(); 
36         System.out.println("SubmittedFileName:" + name);
37         //截取文件后缀名
38         String ext = name.substring(name.lastIndexOf("."),name.length());
39         /**
40          * 获取文件头部信息(目的是从头部信息中截取文件名或扩展名)
41          */    
42 //        String name=part.getHeader("content-disposition");
43 //        System.out.println("name:"+name);        
44 //        //获取文件后缀名
45 //        //form-data; name="upload"; filename="C:\Users\Administrator\Desktop\error.PNG"
46 //        //需要提取从最后一个点后的后缀名  由于默认路径字符串里最后带有引号 则需要使用length()-1
47 //        String ext = name.substring(name.lastIndexOf("."),name.length()-1);
48 //        System.out.println("ext:"+ext);
49         //写出完整的文件路径
50         String filename = root+"\\"+UUID.randomUUID().toString()+ext;
51         System.out.println("filename:"+filename);
52         //写入文件
53          part.write(filename);
54          
55     }
56 
57 
58 }