MMyiyuan 2014-01-24 05:16 采纳率: 0%
浏览 5667

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 (?)

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺