1

I have been trying to upload image to database. So I made a browse button to go through directories and get the image. Suppose if no image is selected, a default image will be uploaded to the database. The image is stored in a folder named 'resources' under source folder.In the image, helper is the folder containting .java files and below is the resources folderhelper directory contains .java files and below is the resource folder in this image I am uploading the image as FileInputStream and the code is

File pic=new File(imgloc);
FileInputStream fis=new FileInputStream(pic);

If the image is not selected to upload a default image my code is

File pic=new File(this.getClass().getResource("/resources/profile1.png").getFile());
FileInputStream fis=new FileInputStream(pic);

And my query to database is

    ps.setBinaryStream(1,fis ,(int) pic.length()); //While substituing for '?' in query 

After build to jar it produce FileNotFoundException when trying to add default image. May be the question is asked several times I didn't find solution in that. I want read the image as file..Thanks in advance.

Previnkumar
  • 107
  • 3
  • 13

2 Answers2

1

to help illustrate I create a project with a source directory

src

below source I had a package starthere with a class Main

I create another source directory xxx and put in a file a.txt

I also created under xxx a directory called text and put in a file named b.txt

Also I created under src a directory called other and put in a file called c.txt

My code is

    URL url = Main.class.getResource("/a.txt");
    System.out.println(url);
    URL url2 = Main.class.getResource("/text/b.txt");
    System.out.println(url2);
    URL url3 = Main.class.getResource("/other/c.txt");
    System.out.println(url3);

and my output is

D:\temp>java -cp ab.jar starthere.Main

jar:file:/D:/temp/ab.jar!/a.txt

jar:file:/D:/temp/ab.jar!/text/b.txt

jar:file:/D:/temp/ab.jar!/other/c.txt

edit

As you are not wanting to use a File, but simply the InputStream, you can use

InputStream fis = Main.class.getResourceAsStream("/a.txt");

and then set using ps.setBinaryStream(1, fis);

Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Yes bro, I tried this thing. After this code i used `File pic=new File(url.to URI);` because i need to load it to database Worked in code bro But not in jar. It showed exception `illegal argument exception: URI is not hierarchical`. – Previnkumar Jul 06 '17 at 05:58
  • I am not following you. The error is a completely different error. Also this code `File pic=new File(url.to URI);` does not make sense at all – Scary Wombat Jul 06 '17 at 06:02
  • Bro sorry for mistakes made by me, But my question itself is i want to get image as `file` Then only i can add it to database – Previnkumar Jul 06 '17 at 06:06
  • As per my code, are you getting an URL? This is the first step – Scary Wombat Jul 06 '17 at 06:08
  • yeah bro. I got `url`. Then kindly say what shld i do – Previnkumar Jul 06 '17 at 06:09
  • Working in code bro. But in jar `file:\C:\Users\Previn\Documents\NetBeansProjects\helper\dist\helper.jar!\resources\profile1.png java.io.FileNotFoundException: file:\C:\Users\Previn\Documents\NetBeansProjects\helper\dist\helper.jar!\resources\profile1.png (The filename, directory name, or volume label syntax is incorrect)`. This exception arises. – Previnkumar Jul 06 '17 at 06:17
  • Well as you don't really want the file, your just want the inputstream, use `getResourceAsStream` instead and then use https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setBinaryStream(int,%20java.io.InputStream) – Scary Wombat Jul 06 '17 at 06:20
0

Consider following package structure

/
/testing_package
/testing_package/Main.java
/testing_package/Image.png

Now Here is a program to get Image.png and put it in database

public class Main {
    public static void main(String...args) {
        System.out.println(Main.class.getResource("image.jpg"));
        //PreparedStatement as ps
        ps.setBinaryStream(1, Main.class.getResourceAsStream("image.jpg")));
    }
}

Thing to keep in mind
When using getResource(...) or getResourceAsStream(...) use relative location to the class instance whose function you are invoking.

Have a look at this answer too

afzalex
  • 8,598
  • 2
  • 34
  • 61