2 mmyiyuan MMyiyuan 于 2014.01.24 13:16 提问

java io File not found 找不到系统指定的文件,工程目录下有此文件源码如下

PrintWriter out = response.getWriter();
response.setContentType("text/html");
// 图片上传路径
String uploadPath = request.getSession().getServletContext().getRealPath("/") + "upload/images/";
System.out.println("uploadPath=" + uploadPath);
// 图片临时上传路径
String tempPath = request.getSession().getServletContext().getRealPath("/") + "upload/images/temp/";
System.out.println("tempPath=" + tempPath);
// 图片网络相对路径
String imagePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
System.out.println("imagePath=" + imagePath);
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem file = (FileItem) i.next();
// 获得文件名,这个文件名是用户上传时用户的绝对路径:
String sourcefileName = file.getName();
if (sourcefileName != null && (sourcefileName.endsWith(".jpg") || sourcefileName.endsWith(".gif"))) {
// 在这里可以记录用户和文件信息,生成上传后的文件名
String destinationfileName = null;
Random rd = new Random();
Calendar time = Calendar.getInstance();
if (sourcefileName.endsWith(".jpg")) {
destinationfileName = String.valueOf(time.get(Calendar.YEAR)) + String.valueOf(time.get(Calendar.MONTH)) + String.valueOf(time.get(Calendar.DAY_OF_MONTH)) + String.valueOf(time.get(Calendar.HOUR_OF_DAY)) + String.valueOf(time.get(Calendar.MINUTE)) + String.valueOf(time.get(Calendar.SECOND)) + String.valueOf(rd.nextInt(100)) + ".jpg";
} else if (sourcefileName.endsWith(".gif")) {
destinationfileName = String.valueOf(time.get(Calendar.YEAR)) + String.valueOf(time.get(Calendar.MONTH)) + String.valueOf(time.get(Calendar.DAY_OF_MONTH)) + String.valueOf(time.get(Calendar.HOUR_OF_DAY)) + String.valueOf(time.get(Calendar.MINUTE)) + String.valueOf(time.get(Calendar.SECOND)) + String.valueOf(rd.nextInt(100)) + ".gif";
}
PhotoBean pb = new PhotoBean();
PhotoDao pd = new PhotoDao();
File Photopath1 = new File(uploadPath + destinationfileName);
pb.setPhotopath(TpUtil.getBytes(Photopath1));
int result = pd.addPhoto(pb);
file.write(Photopath1);
if (result < 0) {
out.print(sourcefileName + "成功上传!");
// out.print("");

                } else {
                    out.println("上传文件出错,只能上传 *.jpg , *.gif");
                }
                // 跳转到上传成功提示页面
            }
        }
    } catch (Exception e) {
        // 可以跳转出错页面
    }
    out.flush();
    // out.close();

----------------------异常如下-----------------------------------
uploadPath=E:\Tomcat\webapps\UpLoadTest\upload/images/
tempPath=E:\Tomcat\webapps\UpLoadTest\upload/images/temp/
imagePath=http://localhost:80/UpLoadTest/
java.io.FileNotFoundException: E:\Tomcat\webapps\UpLoadTest\upload\images\201402413141853.jpg (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:106)
at com.nycgsweb.db.TpUtil.getBytes(TpUtil.java:50)
at com.nycgsweb.servlet.UploadServlet.doPost(UploadServlet.java:122)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
INSERT INTO Photo(phtot) VALUES (?)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!