0

Until now I did saving image into the webapp directory and its path into database.

But now am trying to save the image outside of the webapp so that if I deploy my new war files then my old files folder will not be deleted.

From my below code my image file is correctly saving into the specified folder outside of the webapp but i don't know how to retrieve that image into my jsp page.

I tried like this

<img src="www.myproject.com/struts2project/files/smile.jpg/>"

but this is wrong. I am not getting my image to be display into my jsp page.

Below code is working fine for uploading image into absolute path but my problem is how to retrieve that image?

   `fileSystemPath= "/files"; 
         try{                
             File destFile  = new File(fileSystemPath, thempicFileName);
                 FileUtils.copyFile(thempic, destFile);
                 String path=fileSystemPath+"/"+thempicFileName; 
                 theme=dao.getThemeById(themId); 
                 theme.setThemeScreenshot(path);
                 theme.setThemeName(theme.getThemeName());
                 theme.setThemeCaption(theme.getThemeCaption());
                 dao.saveOrUpdateTheme(theme);

         }catch(IOException e){
            e.printStackTrace();
            return INPUT;
         }`

Kindly help me...

I hope I'm being clear on what I need, let me know if I am not and I'll try to explain in another way.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

As you say . . . this question describes what you need to do. I guess what you need to know is how to best achieve this with struts 2. Here's what's going on.

In your tag:

That url is being routed to your struts 2 application. Correct? The context is "struts2project".
One of the solutions offered by the referenced question is to use Tomcat's ability to serve static requests and configure tomcat to know about this other document root that holds your images. I think this is a great solution.

If you want to keep it inside of struts2, I think you're best option is to use a dedicated "image streaming from that other place" action that get's an InputStream to the image, then uses the Struts2 Stream Result result type. That result type lets you specify an adhoc InputStream. It also helps you set the appropriate headers. Note, the header values on that documentation page are for downloading the file, so you don't want those values. They would force the browser to open a save as dialog for the image, I think.

Community
  • 1
  • 1
chad
  • 7,369
  • 6
  • 37
  • 56
  • i have hosted my application on Shared server? Can go for the first option which you have suggested? I do'nt think so in shared server i can edit the tomcat `server.xml` file. --> `One of the solutions offered by the referenced question is to use Tomcat's ability to serve static requests and configure tomcat to know about this other document root that holds your images` –  Dec 17 '12 at 15:46
  • I'm pretty sure you can leverage the tomcat default servlet regardless. You might be able to do it from your struts2 web app even. Struts 2 is just one servlet (filter) mapping. You could add another that leverages the tomcat default servlet. You could also write your own servlet, but the struts 2 stream result is much more appropriate I think. – chad Dec 17 '12 at 16:36
  • please tell me how can i write my own struts2 stream result which will solve my above problem? Please give me any programming example –  Dec 17 '12 at 17:08
  • That link to the Stream Result tells you what to do. You just need to write an action that catches the request for the image, opens an InputStream on the file, then uses that result type. – chad Dec 17 '12 at 17:21
  • is this example is what you are suggesting for me http://www.mkyong.com/struts2/struts-2-dynamic-image-example/ –  Dec 17 '12 at 17:57
0

You are already using absolute paths, just use a location outside of your web application:

String destinationDir = "/path/to/my/directory/";
File file = new File(destinationDir + item.getName());
Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
  • this is fine..my images are inserting correctly outside of the webapp folder but how can i reterive that image path to show in my jsp page eg. `` –  Dec 17 '12 at 14:37
  • You must decide if your images are to be served by your application server (Tomcat or whatever), or if they will be statically served by your webserver. That has nothing to do with struts – leonbloy Dec 17 '12 at 14:43
  • i am looking for this type of answer http://stackoverflow.com/questions/1812244/simplest-way-to-serve-static-data-from-outside-the-application-server-in-a-java –  Dec 17 '12 at 15:10