首页新闻找找看学习计划

各位前辈,各位大哥请看一看:关于servlet3.0文件上传filename为什么是绝对路径

0
悬赏园豆:10 [待解决问题]
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="FirstServlet" method="post" enctype="multipart/form-data">
11     <input type="file" name="file"><br>
12     
13     <input type="submit" value="submit">
14 </form>
15 
16 </body>
17 </html>

这是我的jsp代码,还有我的servlet代码为:

 1 package servlet;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.PrintWriter;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.MultipartConfig;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.Part;
14 
15 /**
16  * Servlet implementation class FirstServlet
17  */
18 @WebServlet(name="FirstServlet",urlPatterns={"/FirstServlet","/myservlet"})
19 @MultipartConfig()
20 
21 public class FirstServlet extends HttpServlet {
22     private static final long serialVersionUID = 1L;
23     
24     protected void doPost(HttpServletRequest request, HttpServletResponse response)
25             throws ServletException, IOException {
26         Part part=request.getPart("file");
27         
28         String uploadedPath = request.getSession().getServletContext().getRealPath("/upload");
29 
30         String value=part.getHeader("content-disposition");
31         
32         System.out.println(value);
33         
34         String filename=value.substring(value.lastIndexOf("=")+2,value.length()-1);
35         
36         System.out.println(filename);
37         
38         part.write(uploadedPath+"/"+filename);
39     }
40 
41     
42 
43 
44 }

我看网上的一些人说输出的请求头信息的内容是form-data; name="file"; filename="a.txt"

之类的,而我的输出的是form-data; name="file"; filename="G:\Users\Wenge\Desktop\aa.txt"

这是为什么呢?

还有就是如果这样了,那么最后一句part.write(uploadedPath+"/"+filename);就会出错,报(文件名、目录名或卷标语法不正确。)应该是和我的filename是绝对路径有关系

我应该怎么做才能将filename变成网上说的那样呢???!!!

希望能够得到您的解答,先谢谢了!!!

太极芋头的主页 太极芋头 | 初学一级 | 园豆:192
提问于:2012-11-24 15:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册