Web开发中⽂件上传是出现的:
java.io.FileNotFoundException:。。。
1. <span >
2. </span>
<span ><span >⼀环境的搭建:</span></span>
eclipase + javaEE +  commons-fileupload-1.2.1.jar + commons-io-2.0.jar  +  tomcat 7.0.68
⼆⽂件上传的servlet 代码实现!
1. package com.baidu.fileupload.servlet;
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;
inputtypefile不上传文件8. import java.util.List;
9.
10. import javax.servlet.ServletException;
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.
16. import org.apachemons.fileupload.FileItem;
17. import org.apachemons.fileupload.FileUploadException;
18. import org.apachemons.fileupload.disk.DiskFileItemFactory;
19. import org.apachemons.fileupload.servlet.ServletFileUpload;
20.
21. @WebServlet("/uploadServlet")
22. public class UploadServlet extends HttpServlet {
23.
24. private static final long serialVersionUID = 1L;
25.
26. protected void doPost(HttpServletRequest request,
27. HttpServletResponse response) throws ServletException, IOException {
30. DiskFileItemFactory factory = new DiskFileItemFactory();
31.
32.
33. factory.setSizeThreshold(1024*500);
34. File tempDirectory = new File("d:\\tempDirectory");
35. factory.setRepository(tempDirectory);
36.
37. ServletFileUpload upload = new ServletFileUpload(factory);
38.
39. try {
40. List<FileItem> items = upload.parseRequest(request);
41. for(FileItem item:items){
42. if(item.isFormField()){
43. String name = FieldName();
44. String value = String();
45.
46. System.out.println( name + " : " + value);
47. }else{
48. String fieldName = FieldName();
49. String fileName = Name();
50. String contentType = ContentType();
51. long sizeInBytes = Size();
52.
53. System.out.println(fieldName);
54. System.out.println(fileName);
55. System.out.println(contentType);
56. System.out.println(sizeInBytes);
57.
58.
59. InputStream in = InputStream();
60. byte[] buffer = new byte[1024];
61. int len = 0;
62.
63. fileName = "d:\\files\\" + fileName;
64. System.out.println(fileName);
65.
66. OutputStream out = new FileOutputStream(fileName);
67.
69. out.write(buffer, 0, len);
70.
71. }
72. out.close();
73. in.close();
74. }
75. }
76.
77. } catch (FileUploadException e) {
78. e.printStackTrace();
79. }
80. }
81. }
三  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" "/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.
11. <form action="uploadServlet" method="post" enctype="multipart/form-data">
12.
13.
14. File: <input type="file" name="file" />
15. <br><br>
16. <br><br>
17. DESC: <input type="text" name="desc" />
18. <br><br>
19. <br><br>
20. <input type="submit" value="Submit" />
21. </form>
23. </body>
24. </html>
四,运⾏过程中出现的问题
五,解决的办法:
进过⼀番百度,⽆意中看到⼀位朋友提到,不同的浏览器对上传的⽂件名的处理⽅式不太⼀样,进过⼀番实验,搞定,且增加了⼀⾏代码!
1. package com.baidu.fileupload.servlet;
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. import java.util.List;
9.
10. import javax.servlet.ServletException;
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.
16. import org.apachemons.fileupload.FileItem;
17. import org.apachemons.fileupload.FileUploadException;
18. import org.apachemons.fileupload.disk.DiskFileItemFactory;
19. import org.apachemons.fileupload.servlet.ServletFileUpload;
20.
21. @WebServlet("/uploadServlet")
22. public class UploadServlet extends HttpServlet {
23.
24. private static final long serialVersionUID = 1L;
25.
26. protected void doPost(HttpServletRequest request,
27. HttpServletResponse response) throws ServletException, IOException {
29.
30. DiskFileItemFactory factory = new DiskFileItemFactory();
31.
32.
33. factory.setSizeThreshold(1024*500);
34. File tempDirectory = new File("d:\\tempDirectory");
35. factory.setRepository(tempDirectory);
36.
37. ServletFileUpload upload = new ServletFileUpload(factory);
38.
39. upload.setSizeMax(1024 * 1024 * 5);
40.
41. try {
42. List<FileItem> items = upload.parseRequest(request);
43. for(FileItem item:items){
44. if(item.isFormField()){
45. String name = FieldName();
46. String value = String();
47.
48. System.out.println( name + " : " + value);
49. }else{
50. String fieldName = FieldName();
51. String fileName = Name();
52. String contentType = ContentType();
53. long sizeInBytes = Size();
54.
55. System.out.println(fieldName);
56. System.out.println(fileName);
57. System.out.println(contentType);
58. System.out.println(sizeInBytes);
59.
60. //不同的浏览器提交的⽂件名是不⼀样的,有些浏览器提交的⽂件名是带有路径的,如:c:\,⽽有些只是单纯的⽂件名,如:a.txt
61. //处理获取到的上传⽂件的⽂件名的路径部分,只保留⽂件名部分
62. fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
63.
64. InputStream in = InputStream();
65. byte[] buffer = new byte[1024];
66. int len = 0;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。