Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

java - FileOutputStream crashes with "open failed: EISDIR (Is a directory)" error when downloading image

I'm trying to download an iamge from the internet, Here is the code:

try {
                String imgURL = c.imgURL;
                String imgPATH = c.imgPATH;
                URL url = new URL(imgURL);
                URLConnection conexion = url.openConnection();
                conexion.connect();
                int lenghtOfFile = conexion.getContentLength();
                try {
                    File f = new File(imgPATH);
                    f.mkdirs();

                    BufferedInputStream input = new BufferedInputStream(url.openStream());
                    BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(imgPATH), 8192); // CRASH HERE

                    byte data[] = new byte[8192];
                    long total = 0;
                    int count = 0;
                    int updateUILimiter = 0;
                    while ((count = input.read(data)) != -1) {
                        total += count;

                        if (updateUILimiter == 20)
                            // publishProgress((int) (total * 100 / lenghtOfFile));
                            updateUILimiter = 0;
                        else
                            updateUILimiter++;

                        output.write(data, 0, count);

                        if (isCancelled()) {
                            output.flush();
                            output.close();
                            input.close();
                            return null;
                        }

                    }
                    output.flush();
                    output.close();
                    input.close();
                } catch (Exception e) {
                    c.imgPATH = "";
                    return null;
                }


            } catch (Exception e) {
                c.imgPATH = "";
                return null;
            }

Here is the error message:

/mnt/sdcard/tmp/3.png: open failed: EISDIR (Is a directory)

Why is this?

" /mnt/sdcard/tmp/" exists.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

3.png is a directory, because you make it so by calling f.mkdirs();. Try f.getParentFile().mkdirs() instead. From the documentation:

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

(emphasis mine). In other words, the entire path contained in the File instance f is taken to be a directory name, up to and including the final part (3.png in the example output).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...