import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class FileUploadServlet */ public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决post请求乱码 request.setCharacterEncoding("utf-8"); /*String name = request.getParameter("name"); String photo = request.getParameter("photo"); System.out.println(name+"--"+photo); */ //获取一个流 /*ServletInputStream in = request.getInputStream(); String str = IOUtils.toString(in); System.out.println(str);*/ //获取一个工厂类实例 DiskFileItemFactory factory=new DiskFileItemFactory(); //创建解析器实例 ServletFileUpload fileUpload=new ServletFileUpload(factory); try { //解析request List<FileItem> fileItems = fileUpload.parseRequest(request); //System.out.println(fileItems.size()); //遍历fileItems,读取表单的信息 for (FileItem fileItem : fileItems) { //判断当前表单项是否是一个普通表单项 if(fileItem.isFormField()) { //获取属性名 String fieldName = fileItem.getFieldName(); //获取属性值 String value = fileItem.getString("utf-8"); System.out.println(fieldName+"="+value); }else { //如果是文件表单项 //获取文件的大小 long size = fileItem.getSize(); //获取文件的类型 String contentType = fileItem.getContentType(); //获取文件的名字 String name = fileItem.getName(); //获取表单项的属性名 String fieldName = fileItem.getFieldName(); System.out.println("文件的大小:"+size); System.out.println("文件的类型:"+contentType); System.out.println("文件的名字:"+name); System.out.println("表单项name属性名:"+fieldName); //获取ServletContext对象 ServletContext context = this.getServletContext(); //获取项目的真实路径 String path = context.getRealPath("/upload"); //判断路径是否存在 File file=new File(path); if(!file.exists()) { //如果不存在该路径,则创建一个路径 file.mkdirs(); } System.out.println(path); //将文件写入到磁盘中 fileItem.write(new File(path+"/"+name)); } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
建议打印出完整的path+"/"+name
文件路径看是否有问题
对不起刚才没发错误信息
name=xx java.io.FileNotFoundException: D:\java ee\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WEB_FileUpload1\upload\C:\愤怒.png (文件名、目录名或卷标语法不正确。) 文件的大小:136008 文件的类型:image/png 文件的名字:C:\愤怒.png 表单项name属性名:photo D:\java ee\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WEB_FileUpload1\upload at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.<init>(FileOutputStream.java:213) at java.io.FileOutputStream.<init>(FileOutputStream.java:162) at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:417) at com.atguigu.FileUpload1.FileUploadServlet.doPost(FileUploadServlet.java:93) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
大哥,D:\java ee\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WEB_FileUpload1\upload\C:\愤怒.png (文件名、目录名或卷标语法不正确。)是这里的问题吗?
@妈妈说名字要高调: 路径中多了C:\
,需要去掉
@dudu: 好了,谢谢大哥。
经过测试在获取文件名那行代码下加入如下代码就能完美解决问题,希望遇到这个问题的同学别再走弯路。
if(name.contains("\\")) { //如果包含则截取字符串 name=name.substring(name.lastIndexOf("\\")+1); }