我目前正在尝试上传多个文件,并且这些文件正在正确上传到目标目录中。但是在日志中它给了我这个错误:
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的新手,我不确定为什么它会给我这个错误。 我该如何调试呢?
答案 0 :(得分:10)
在某些情况下,filename
看起来为空或为空,因此File outputPath=new File(uploadDirPath + File.separator + fileName);
将成为目录,此处new FileOutputStream(outputPath);
您尝试写入目录而不是文件。因此,您应该检查filename
是否为空。
答案 1 :(得分:5)
您的代码中有很多错误。例如,您只关闭最后 InputStream
和OutputStream
s;你应该关闭它们。你在这里做事的方式,你有资源泄漏。
第二,这是2015年;因此请删除File
并使用java.nio.file;另外,请使用try-with-resources。
第三:现在你上传项目目录;当应用程序在服务器上“实时”运行时,不要这样做,这显然不起作用。
样品:
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);
}
}
答案 2 :(得分:0)
我发生此错误是因为我使用的file.mkdirs()
具有完整的文件名。
假设您的文件路径为emulated/0/test/images/img.jpg
然后从路径中删除最后一部分,并在结果文件中使用file.mkdirs()
File file=new File("emulated/0/test/images/")
if(!file.exists()){
file.mkdirs();
}