FileNotFoundException(是一个目录)
FileNotFoundException (Is a Directory)
我目前正在尝试上传多个文件,这些文件正在正确上传到目标目录中。但是在日志中它给了我这个错误:
java.io.FileNotFoundException: /home/sandeep/java_proj/jee_tut/files (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at com.jee.controller.FileProcessing.uploadFiles(FileProcessing.java:35)
at com.jee.controller.View.doPost(View.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
这是我上传文件的代码:
final String UPLOAD_DIR="files";
String applicationPath="/home/sandeep/java_proj/jee_tut";
String uploadDirPath= applicationPath + File.separator + UPLOAD_DIR;
try{
File fileDir=new File(uploadDirPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
InputStream inputStream= null;
OutputStream outputStream = null;
try{
for(Part part:request.getParts()){
String fileName=getFileName(part);
File outputPath=new File(uploadDirPath + File.separator + fileName);
inputStream = part.getInputStream();
outputStream = new FileOutputStream(outputPath);
int read = 0;
final byte[] bytes = new byte[1024];
while((read=inputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, read);
}
System.out.println(fileName);
}
}catch(Exception e){
e.printStackTrace();
return "";
}
finally{
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
}
return uploadDirPath;
}catch(Exception e){
e.printStackTrace();
return "";
}
由于我是 java 的新手,我不确定为什么会出现此错误。
我该如何调试?
看起来在某些情况下 filename
是空白或空的,所以 File outputPath=new File(uploadDirPath + File.separator + fileName);
将是一个目录,这里 new FileOutputStream(outputPath);
您尝试写入目录而不是文件。所以你应该检查 filename
是否不为空。
您的代码中有很多错误。其一,您只关闭 last InputStream
s 和 OutputStream
s;你应该关闭它们中的每一个。你在这里做事的方式,你有资源泄漏。
其次,这是2015年;因此删除 File
并使用 java.nio.file 代替;另外,使用 try-with-resources。
第三:你现在在项目目录下上传;当应用程序在服务器上运行 "live" 时不要这样做,这显然是行不通的。
样本:
private static final Path BASEDIR = Paths.get("/path/to/upload/directory");
// in the upload method:
Files.createDirectories(BASEDIR);
String fileName;
Path path;
for (final Part part: request.getParts()) {
fileName = getFileName(part);
if (fileName.isEmpty())
continue;
path = BASEDIR.resolve(fileName);
try (
final InputStream in = part.getInputStream();
) {
Files.copy(in, path, StandardOpenOption.CREATE_NEW);
}
}
这个错误发生在我身上是因为我使用 file.mkdirs()
和完整的文件名。
假设你的文件路径是emulated/0/test/images/img.jpg
然后从路径中删除最后一部分并对结果文件
使用file.mkdirs()
File file = new File("emulated/0/test/images/")
if (!file.exists()) {
file.mkdirs();
}
我目前正在尝试上传多个文件,这些文件正在正确上传到目标目录中。但是在日志中它给了我这个错误:
java.io.FileNotFoundException: /home/sandeep/java_proj/jee_tut/files (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at com.jee.controller.FileProcessing.uploadFiles(FileProcessing.java:35)
at com.jee.controller.View.doPost(View.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
这是我上传文件的代码:
final String UPLOAD_DIR="files";
String applicationPath="/home/sandeep/java_proj/jee_tut";
String uploadDirPath= applicationPath + File.separator + UPLOAD_DIR;
try{
File fileDir=new File(uploadDirPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
InputStream inputStream= null;
OutputStream outputStream = null;
try{
for(Part part:request.getParts()){
String fileName=getFileName(part);
File outputPath=new File(uploadDirPath + File.separator + fileName);
inputStream = part.getInputStream();
outputStream = new FileOutputStream(outputPath);
int read = 0;
final byte[] bytes = new byte[1024];
while((read=inputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, read);
}
System.out.println(fileName);
}
}catch(Exception e){
e.printStackTrace();
return "";
}
finally{
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
}
return uploadDirPath;
}catch(Exception e){
e.printStackTrace();
return "";
}
由于我是 java 的新手,我不确定为什么会出现此错误。 我该如何调试?
看起来在某些情况下 filename
是空白或空的,所以 File outputPath=new File(uploadDirPath + File.separator + fileName);
将是一个目录,这里 new FileOutputStream(outputPath);
您尝试写入目录而不是文件。所以你应该检查 filename
是否不为空。
您的代码中有很多错误。其一,您只关闭 last InputStream
s 和 OutputStream
s;你应该关闭它们中的每一个。你在这里做事的方式,你有资源泄漏。
其次,这是2015年;因此删除 File
并使用 java.nio.file 代替;另外,使用 try-with-resources。
第三:你现在在项目目录下上传;当应用程序在服务器上运行 "live" 时不要这样做,这显然是行不通的。
样本:
private static final Path BASEDIR = Paths.get("/path/to/upload/directory");
// in the upload method:
Files.createDirectories(BASEDIR);
String fileName;
Path path;
for (final Part part: request.getParts()) {
fileName = getFileName(part);
if (fileName.isEmpty())
continue;
path = BASEDIR.resolve(fileName);
try (
final InputStream in = part.getInputStream();
) {
Files.copy(in, path, StandardOpenOption.CREATE_NEW);
}
}
这个错误发生在我身上是因为我使用 file.mkdirs()
和完整的文件名。
假设你的文件路径是emulated/0/test/images/img.jpg
然后从路径中删除最后一部分并对结果文件
使用file.mkdirs()
File file = new File("emulated/0/test/images/")
if (!file.exists()) {
file.mkdirs();
}