0

Hello im trying to do something like this:

 private Byte[] getImage() throws IOException {

        String imageUrl = ServletContext.class.getClassLoader()
                .getResource("static/anImage.jpg")
                .getFile();

        Byte[] byteObject = new Byte[imageUrl.getBytes().length];

        int i = 0;
        for (Byte b : imageUrl.getBytes()){
            byteObject[i++] = b;
        }
        return byteObject;
    }

But it's wrong. So how to pick up a file from specific directory? Thanks.

ps. I can do something like this:

 File file = new File("image.jpg");
        byte[] content = Files.readAllBytes(file.toPath());

But still its a path only from the main folder. Dont know how to program for the resources/images folder.

4
  • You can read the file as a Resource then get inputstream from resource and covert to bytes Commented Jul 22, 2020 at 14:37
  • ok. What excatly is the classpath:? Its some kind of folder? resources? Commented Jul 22, 2020 at 15:23
  • Thanks, this is an excelent. I would accept it as an answer if you will post it. Commented Aug 21, 2020 at 14:49
  • Done! Deleted the comment and added an answer. Commented Aug 21, 2020 at 15:01

2 Answers 2

1

In Spring you can use Resources to achieve your goal:

Resource resource = new ClassPathResource("static/anImage.jpg"); 
byte[] bytes = resource.getInputStream().readAllBytes();
Sign up to request clarification or add additional context in comments.

Comments

1

Don't reinvent the wheel and use e.g. Apache Commons for convert a File to byte array. Read more FileUtils.readFileToByteArray(File input)

The way you loading resources seems to be the proper one.

Ensure that the loaded file is in proper location (src/main/resources).

Do you have any particular error or stack trace which describes the issue.

5 Comments

Dzięki za pomoc, będę pisał po ang, żeby każdy rozumiał. Thanks, didnt know something like FileUtils exists. It works good. But the problem is i dont know how to load file from the location like resources/static. The Servlet context doesnt work. So im looking for a way to pick files from directories.
What does "The Servlet context doesnt work" mean? Do you have any exceptions?
There are no exceptions. But the image isnt being uploaded and cant be displayed on the browser. The reason is beacause the imageUrl.getBytes().length carries array of only 84 elements. But when i use File file = new File("image.jpg"); and the image is in the projects main folder i can display it in the browser, it works and its array carries around 200.000 elements. So i still cant load an image from a different path.
Now I see. imageUrl is String so you have bytes from string with path. Try to change imageUrl form string to file: File fimageUrl = new File( getClass().getClassLoader().getResource("database.properties").getFile() ); and it should start working.
I get java.io.FileNotFoundException: File 'C:\Moje\IntelliJ%202020.1.2\WORK\projekt\shopBackend\target\classes\static\gow2.jpg' does not exist Still i see the jpg in target/classes/ static. Ill try the thing with classpath and resources that Edgar wrote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.