简体   繁体   English

如何从源文件夹中读取图像作为FileInputStream?

[英]How to read image as FileInputStream from source folder?

I have been trying to upload image to database. 我一直在尝试将image上传到数据库。 So I made a browse button to go through directories and get the image. 因此,我做了一个browse按钮来browse目录并获取图像。 Suppose if no image is selected, a default image will be uploaded to the database. 假设如果未选择任何图像,则将default image上载到数据库。 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 folder 该图像存储在源文件夹下名为'resources'文件夹中。在该图像中,helper是包含.java文件的文件夹,下面是resources文件夹 helper目录包含.java文件,下面是此图中的资源文件夹 I am uploading the image as FileInputStream and the code is 我将图像上传为FileInputStream ,代码为

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. 生成jar后,在尝试添加默认图像时会产生FileNotFoundException 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. 我想将图像读取为文件。。谢谢。

to help illustrate I create a project with a source directory 为了帮助说明我用源目录创建了一个项目

src

below source I had a package starthere with a class Main 下面源我有一个包starthere一类Main

I create another source directory xxx and put in a file a.txt 我创建另一个源目录xxx并放入文件a.txt

I also created under xxx a directory called text and put in a file named b.txt 我还在xxx下创建了一个名为text的目录,并放入了名为b.txt的文件中

Also I created under src a directory called other and put in a file called c.txt 我也在src下创建了一个名为other的目录,并放入了一个名为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 D:\\ temp> java -cp ab.jar starthere.Main

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

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

jar:file:/D:/temp/ab.jar!/other/c.txt jar:文件:/ 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,因此可以使用

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

and then set using ps.setBinaryStream(1, fis); 然后使用ps.setBinaryStream(1, fis);

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 现在这是一个获取Image.png并将其放入数据库的程序

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. 使用getResource(...)getResourceAsStream(...)使用要调用其功能的类实例的相对位置。

Have a look at this answer too 也看看这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM