I am currently trying to upload multiple files, and the files are being upload properly into the destination directory. But in the logs it is giving me this error:
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)
This is my code for uploading the files:
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 "";
}
As I am new to java, I'm not sure why it is giving me this error. How do I debug this?
This error occured to me because I was using file.mkdirs()
with complete fileName .
Suppose your file path is emulated/0/test/images/img.jpg
Then remove last part from the path and use file.mkdirs()
on result file
File file = new File("emulated/0/test/images/")
if (!file.exists()) {
file.mkdirs();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With