我目前正在嘗試上傳多個文件,並且正在將文件正確上傳到目標目錄中。但在日誌中它給我這個錯誤:FileNotFoundException(是一個目錄)
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,我不知道爲什麼它給我這個錯誤。 如何調試?
論'線35 FileProcessing.java'你想開一個'FileOutputStream'到一個目錄。你不能那樣做。 – Kayaman
您在2015年使用java.io.File的任何原因?如果是Java 7+,請改爲使用java.nio.file。 – fge
@fge:謝謝你的擡頭。我將開始使用Java 7+ –