1

I wanted to save image to user machine while he is launching my java application. for this i have written code as follows :

BufferedImage image = null;
        try {

            URL url = new URL(logourl);
            image = ImageIO.read(url);

            ImageIO.write(image, "png",new File("/usr/swa.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }

But it is giving me

java.io.FileNotFoundException: /usr/swa.png (Permission denied)

but if i used

ImageIO.write(image, "png",new File("/home/MyUserName/Desktop/applicationName/logo.png"));

Then it saving the image. i dont get why it is not accessing that path if it working for /home/MyUserName/Desktop/applicationName/logo.png this path.

basically when user install application i want fetch image from web url and save it to user's local machine so what i need to to achieve this??

EDIT : Exception corrected

Swap L
  • 698
  • 3
  • 12
  • 27
  • 3
    `/temp/applicationName/logo.png`? Did you mean `/tmp/applicationName/logo.png`? Also, are you sure that this directory exists? – BackSlash May 09 '14 at 07:12
  • Given the error message I'd go for "/temp/applicationName does not exist" -- and in Linux that should be "/tmp" – fge May 09 '14 at 07:12
  • yes this directory is exsits n i have tried like "/usr/local/" also but it giving me same exception. – Swap L May 09 '14 at 07:13
  • Is this this my application not getting access to root ? – Swap L May 09 '14 at 07:16
  • @SwapL That can't be happening. If the directory exists, it means that you don't have permissions to write to that directory. But java would fire a `java.lang.IOEsception: Permission Denied` in that case. So double check, likely your directory doesn't exist. – BackSlash May 09 '14 at 07:16
  • Yes Your right now i have just copied exact path the i got java.io.FileNotFoundException: /usr/swap.png (Permission denied) – Swap L May 09 '14 at 07:18
  • @SwapL In that case you must run your application as root. But I would suggest that you choose a directory that doesn't require root access – BackSlash May 09 '14 at 07:19
  • @BackSlash i dont know which directory are dont require root access. – Swap L May 09 '14 at 07:20
  • @SwapL AFAIK, Every directory under the `/home/yourUserName` directory should not require root access – BackSlash May 09 '14 at 07:22
  • Yes, but how i know there username ? – Swap L May 09 '14 at 07:22
  • 1
    I don't know, but you should be able to find the user's home directory path with `String userHome = System.getProperty("user.home");` – BackSlash May 09 '14 at 07:27
  • @BackSlash Yup Worked , thanks !! – Swap L May 09 '14 at 08:03

1 Answers1

3

You don't have write permission to /usr, or, less probably, the file /usr/swa.png already exists and isn't deletable or writable by you.

user207421
  • 305,947
  • 44
  • 307
  • 483