4

I have a User form wherein users are allowed to upload images..I store those images in tomcat server and the image name in database to retrieve a particular user's image.

My code for storing image is as follows..

String filePath = getServlet().getServletContext().getRealPath("/") +"Images";
File folder = new File(filePath);
if(!folder.exists()){
 folder.mkdir();
 }
 String fileName = userForm.getUploadedFile().getFileName();
 System.out.println("Server path:" +filePath);
File newFile = new File(filePath, fileName);
                                              FileOutputStream fos = new FileOutputStream(newFile);
 fos.write(userForm.getUploadedFile().getFileData());
fos.flush();
fos.close();

now I want to display the stored image in profile page of user after login..

I did something like this in profile page..

<img src="http://localhost:8082/Images/${sessionUser.image}"/> 

But my image is not getting displayed..

Please someone guide..

xyz
  • 139
  • 2
  • 5
  • 15
  • 1
    Are you sure that file is getting uploaded into the specified folder? Can you access it through "My Computer"? Also What's the output of `System.out.println("Server path:" +filepath);`? – zaerymoghaddam Oct 14 '13 at 07:23
  • yes its getting uploaded and "Server Path" gives me D:\Arthi iyer\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\NewStrutsOBL\Images\Koala.jpg – xyz Oct 14 '13 at 07:25
  • 1
    View your profile page source and copy the exact value of `src` property of the `img` tag and paste it directly in your browser address bar. What's the result? Which HTTP error it gives to you? – zaerymoghaddam Oct 14 '13 at 07:31
  • http://localhost:8082/Images/Koala.jpg I did something like this.. it gave me nothing..it says 'Internet explorer cannot display the web page' – xyz Oct 14 '13 at 07:37
  • 1
    Try it in a browser like chrome and check the Network tab of its Developer Tools. What's in the `status` column? What's the response header? – zaerymoghaddam Oct 14 '13 at 07:51
  • 1
    You said you have `localhost:8082/Images/Koala.jpg` in your page but in response to @Ashish you say `localhost:8082/Images/${sessionUser.image}` which one is correct? – zaerymoghaddam Oct 14 '13 at 07:53

2 Answers2

2

As we are trying to access a file(image,video,pdf etc) which is outside the webapp container, we need to create a link from server to that folder. This can be done using 'docBase' and 'path' attribute of Context element. There are several ways of doing this( refer this link for all methods stated in Tomcat documentation) The easiest way I found is placing following code in /apache-tomcat-8.0.36/conf/server.xml file of Tomcat server under Host tag .

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
---------
---------
<Context docBase="/path/of/your/external/folder" path="/your_webapp_name/your/url"/>
---------
---------
</Host>

Now you can easily access files in outside folder in your view page as follow (suppose there is an image named IMAGE.jpeg in /path/of/your/external/folder):

<img src="/your_webapp_name/your/url/IMAGE.jpeg" />

Note:- be aware of whether you are using eclipse configured tomcat server or using tomcat from your terminal. In either case make change to appropriate server.xml file i.e. if you are using eclipse configured tomcat server make changes to server.xml file inside Servers directory of eclipse and not in its home directory where it is installed.

Suyash Tilhari
  • 123
  • 2
  • 10
0

Your web app runs under context /NewStrutsOBL‌ and the link you put is for the root context /.

You should either

change the the link to use context path:

<img src="${pageContext.request.contextPath}/Images/${sessionUser.image}"/>

or put the context in the link (not a good way but working)

<img src="http://localhost:8082/NewStrutsOBL‌/Images/${sessionUser.image}"/>

or use relative path in the link, say if your page file is under the same directory as Images (may be good or not depending on needs):

<img src="Images/${sessionUser.image}"/>

As a side note, you can also change your context path from /NewStrutsOBL‌ to / (simpler way when you run from Eclipse), but this is not related to the question and in general it will be a bad practice to rely on specific context root in your links or code.

As another, unrelated side note, in large enterprise application usually you'll not modify web app content of running application but rather use an external folder or db. But for personal or test application it is fine.

Community
  • 1
  • 1
Fedor Losev
  • 3,244
  • 15
  • 13