1

I am developing a web application where I want to get image from user. So I wrote a Servlet to get file path from user. I'm passing file path as...

localhostApiBase/image?path=/home/userName/Pictures/25.jpg

I deployed my application on localhost and Its working fine. i.e. my application found the file and processed as intended. I'm reading image like this

File oFile = new File(path);    
BufferedImage oImage = ImageIO.read(oFile);

Then I deployed my application to AWS Elastic Beanstalk, and when I'm trying to hit the same Servlet with same path as....

serverApiBase/image?path=/home/userName/Pictures/25.jpg

then

File oFile = new File(path);
oFile.exists(); // returns false
BufferedImage oImage = ImageIO.read(oFile); // throws java.nio.file.NoSuchFileException: /home/userName/Pictures/25.jpg

After some efforts I came to know that it is trying to find file on server (AWS Elastic Beanstalk) not on my (user's) local machine. So it is throwing

       java.nio.file.NoSuchFileException .

Can anyone help me solve the issue. I want users to upload images from their machine, which I will use further....

Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43
Dinesh
  • 35
  • 9

2 Answers2

0

Save the user uploaded file on the server storage like this,

        File targetFile = new File(filePath);
        OutputStream outStream = new FileOutputStream(targetFile);
        outStream.write(buffer);
        outStream.close();

and access the file like this,

        server_ip/image?path=filePath
Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43
0

If you want to get the image from the user during run-time you have to create some upload logic. Here is a good explanation: How to upload files to server using JSP/Servlet?

If the image is static, i.e. part of the application use the aproach mentioned by Drux.

Community
  • 1
  • 1
Thomas Stets
  • 3,015
  • 4
  • 17
  • 29