10

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?

Sandeep Rao
  • 341
  • 3
  • 9
  • 22

3 Answers3

12

Looks like in some cases filename is blank or null so File outputPath=new File(uploadDirPath + File.separator + fileName); will be a directory and here new FileOutputStream(outputPath); you try to write to a directory not to a file. So you should check if filename is not blank.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • 1
    I also wanted to point out that a path that looks like a file, may in fact be a directory. For example "home/test/file.png" may be a directory if there was an error in your code by calling .mkdirs() on the file instead of it's parent directory. – TheIT Mar 15 '17 at 22:54
6

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();
}
Boken
  • 4,825
  • 10
  • 32
  • 42
Manohar
  • 22,116
  • 9
  • 108
  • 144
5

You have many errors in your code. For one, you only close the last InputStreams and OutputStreams; you should close each of them. The way you do things here, you have a resource leak.

Second, this is 2015; therefore drop File and use java.nio.file instead; plus, use try-with-resources.

Third: right now you upload in the project directory; don't do that when the application runs "live" on a server, this obviously won't work.

Sample:

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);
    }
}
fge
  • 119,121
  • 33
  • 254
  • 329