Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)


  1. <span style="font-family: Arial, Helvetica, sans-serif; ">

  2. </span>

<span style="font-family: Arial, Helvetica, sans-serif; "><span style="color:#ff0000;">一 环境的搭建:</span></span>

Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)

eclipase + javaEE +  commons-fileupload-1.2.1.jar + commons-io-2.0.jar   +  tomcat 7.0.68

Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)

二  文件上传的servlet 代码实现!


  1. package com.baidu.fileupload.servlet;

  2. import java.io.File;

  3. import java.io.FileOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.OutputStream;

  7. import java.util.List;

  8. import javax.servlet.ServletException;

  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 org.apache.commons.fileupload.FileItem;

  14. import org.apache.commons.fileupload.FileUploadException;

  15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;

  16. import org.apache.commons.fileupload.servlet.ServletFileUpload;

  17. @WebServlet("/uploadServlet")

  18. public class UploadServlet extends HttpServlet {

  19. private static final long serialVersionUID = 1L;

  20. protected void doPost(HttpServletRequest request,

  21. HttpServletResponse response) throws ServletException, IOException {

  22. DiskFileItemFactory factory = new DiskFileItemFactory();

  23. factory.setSizeThreshold(1024*500);

  24. File tempDirectory = new File("d:\\tempDirectory");

  25. factory.setRepository(tempDirectory);

  26. ServletFileUpload upload = new ServletFileUpload(factory);

  27. try {

  28. List<FileItem> items = upload.parseRequest(request);

  29. for(FileItem item:items){

  30. if(item.isFormField()){

  31. String name = item.getFieldName();

  32. String value = item.getString();

  33. System.out.println( name + " : " + value);

  34. }else{

  35. String fieldName = item.getFieldName();

  36. String fileName = item.getName();

  37. String contentType = item.getContentType();

  38. long sizeInBytes = item.getSize();

  39. System.out.println(fieldName);

  40. System.out.println(fileName);

  41. System.out.println(contentType);

  42. System.out.println(sizeInBytes);

  43. InputStream in = item.getInputStream();

  44. byte[] buffer = new byte[1024];

  45. int len = 0;

  46. fileName = "d:\\files\\" + fileName;

  47. System.out.println(fileName);

  48. OutputStream out = new FileOutputStream(fileName);

  49. while((len = in.read(buffer)) != -1){

  50. out.write(buffer, 0, len);

  51. }

  52. out.close();

  53. in.close();

  54. }

  55. }

  56. } catch (FileUploadException e) {

  57. e.printStackTrace();

  58. }

  59. }

  60. }

三  JSP 编写

update.jsp


  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="uploadServlet" method="post" enctype="multipart/form-data">

  11. File: <input type="file" name="file" />

  12. <br><br>

  13. <br><br>

  14. DESC: <input type="text" name="desc" />

  15. <br><br>

  16. <br><br>

  17. <input type="submit" value="Submit" />

  18. </form>

  19. </body>

  20. </html>

四, 运行过程中出现的问题

Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)

Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)

 五, 解决的办法:

进过一番百度, 无意中看到一位朋友提到,不同的浏览器对上传的文件名的处理方式不太一样 ,进过一番实验,搞定,且增加了一行代码!


  1. package com.baidu.fileupload.servlet;

  2. import java.io.File;

  3. import java.io.FileOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.OutputStream;

  7. import java.util.List;

  8. import javax.servlet.ServletException;

  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 org.apache.commons.fileupload.FileItem;

  14. import org.apache.commons.fileupload.FileUploadException;

  15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;

  16. import org.apache.commons.fileupload.servlet.ServletFileUpload;

  17. @WebServlet("/uploadServlet")

  18. public class UploadServlet extends HttpServlet {

  19. private static final long serialVersionUID = 1L;

  20. protected void doPost(HttpServletRequest request,

  21. HttpServletResponse response) throws ServletException, IOException {

  22. DiskFileItemFactory factory = new DiskFileItemFactory();

  23. factory.setSizeThreshold(1024*500);

  24. File tempDirectory = new File("d:\\tempDirectory");

  25. factory.setRepository(tempDirectory);

  26. ServletFileUpload upload = new ServletFileUpload(factory);

  27. upload.setSizeMax(1024 * 1024 * 5);

  28. try {

  29. List<FileItem> items = upload.parseRequest(request);

  30. for(FileItem item:items){

  31. if(item.isFormField()){

  32. String name = item.getFieldName();

  33. String value = item.getString();

  34. System.out.println( name + " : " + value);

  35. }else{

  36. String fieldName = item.getFieldName();

  37. String fileName = item.getName();

  38. String contentType = item.getContentType();

  39. long sizeInBytes = item.getSize();

  40. System.out.println(fieldName);

  41. System.out.println(fileName);

  42. System.out.println(contentType);

  43. System.out.println(sizeInBytes);

  44. //不同的浏览器提交的文件名是不一样的,有些浏览器提交的文件名是带有路径的,如:c:\nihao\a.txt,而有些只是单纯的文件名,如:a.txt

  45. //处理获取到的上传文件的文件名的路径部分,只保留文件名部分

  46. fileName = fileName.substring(fileName.lastIndexOf("\\")+1);

  47. InputStream in = item.getInputStream();

  48. byte[] buffer = new byte[1024];

  49. int len = 0;

  50. fileName = "d:\\files\\" + fileName;

  51. System.out.println(fileName);

  52. OutputStream out = new FileOutputStream(fileName);

  53. while((len = in.read(buffer)) != -1){

  54. out.write(buffer, 0, len);

  55. }

  56. out.close();

  57. in.close();

  58. }

  59. }

  60. } catch (FileUploadException e) {

  61. e.printStackTrace();

  62. }

  63. }

  64. }

显示推荐内容

上一篇:华为存储OceanStor5500 v3构建iSCSI详解


下一篇:flash上传控件跨域