我想从路径字符串创建指向我计算机上的jpg文件的输入流。但是,我收到此错误:
java.io.FileNotFoundException: C:\ Users \ bnbih \ Downloads \ tmp.zip \ tmp \ 2227795.jpg(系统不能 找到指定的路径)
但是,该文件确实存在于我的计算机上,我可以从同一路径创建一个File对象。
这是我的所有参考代码:
/**detects if the user uploaded ZIP file*/
if(zipFile.getSize() != 0){
Path zipPath = Paths.get(zipFile.getSubmittedFileName());//get the path for the zip file
String zipFileName = zipPath.getFileName().toString();//gets the zip file name with ext
//create a file system to manipulate the files
FileSystem fs = FileSystems.newFileSystem(zipPath, null);
/**create an iterator over all the individual image files
* based on the assumption the the zip file structure is testZIP->testZIP->individual images*/
Iterator<Path> it = Files.walk(fs.getPath("/" + getRawName(zipFileName))).iterator();
//while there're images in the ZIP
while(it.hasNext()){
//gets the path of the current image
Path currentImage = it.next();
//constructs an image file based on path (works fine)
File currentFile = new File(zipFile.getSubmittedFileName(), currentImage.toString());
//holds the type of the file (to test if its a JPG image)
String fileType= new MimetypesFileTypeMap().getContentType(currentFile);
//checks if current file is JPG image
if(fileType.equals("image/jpeg")){
//create path for the current path
Path pt = Paths.get(zipFile.getSubmittedFileName(), currentImage.toString());
//create input stream based on pt (where error appears even though it is the same path as the File object above)
InputStream curImgStr = new FileInputStream(pt.toString());
/**make input stream for the file to be inserted as blob in SQL*/
//fills zipMap with GUID and image file pair
zipMap.put(getRawName(currentImage.getFileName().toString()), curImgStr);
//Files.copy(currentImage, Paths.get(System.getProperty("user.home"), "/tesoo/foo" + i + ".jpg"), REPLACE_EXISTING);
}//end of if that checks if file is a JPG image
}//end of while
}//end of if that checks if ZIP was uploaded
这是整个错误:
java.io.FileNotFoundException: C:\ Users \ bnbih \ Downloads \ tmp.zip \ tmp \ 2227795.jpg(系统不能 在java.io.FileInputStream.open0(Native。找到指定的路径) 方法)在java.io.FileInputStream.open(FileInputStream.java:195)at at java.io.FileInputStream。(FileInputStream.java:138)at java.io.FileInputStream。(FileInputStream.java:93)at FileUploadDBServlet.doPost(FileUploadDBServlet.java:115)at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)at at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 在 org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283) 在 com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 在 org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) 在 org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) 在 org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 在 org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) 在 org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在 org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) 在 org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:591) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:571) 在java.lang.Thread.run(Thread.java:745)
这是我电脑的路径:
C:\ Users \ bnbih \ Downloads \ tmp.zip \ tmp,文件在里面