-1

i am trying to save image outside of my web app directory. i have hosted my application on shared. so i got a root folder to deploy my project. How can i just make a directory outside of my webapp so that i can upload my images on it. and if i redeploy my app then those images will not lost.

eg. i got root space and i pasted all my build folder on it. I want the upload folder not to be lost if I redeploy my app.

fileSystemPath= "/files"; 
 try{                
     File destFile  = new File(fileSystemPath, thempicFileName);
         FileUtils.copyFile(thempic, destFile);
         String path=fileSystemPath+"/"+thempicFileName;  

With the above code a folder is creating in D drive(my current working directory), and in my db file is storing like /files/smg.png And if i try to access that image using <img src="/files/smg.png"/> from my jsp page then i am not geting that image to be display.

Where the project and files are exactly.. with the above code my images are saving in D:\myprojectDir\files and my project location is D:\myprojectDir\projectfolder

THIS PROJECT IS BEING DEVELOP IN STRUTS2

Please suggest me in which way i have to write the code so that i can receive my images on jsp.

Dan
  • 2,086
  • 11
  • 71
  • 137
  • Then you have a lot of examples searchin in Google for: "struts2 streaming image to http response". – francadaval Dec 17 '12 at 08:59
  • @francadaval This is my exact problem http://stackoverflow.com/questions/13911308/unable-to-locate-exact-location-of-image-file-in-jsp-page – Dan Dec 17 '12 at 09:20
  • So do you want to open this folder to http access? You must know that jsp doesn't access to your system files or folders. jsp set a URL () for the web browser to request image to the web server. So or you put images in WebContent (static images) or you can make as I said in my answer. – francadaval Dec 17 '12 at 09:29
  • @francadaval i want just to put my images in any place so that if i redeploy my project then it should not be deleted and i can display that images in any jsp page. How to do this? – Dan Dec 17 '12 at 09:33
  • @francadaval is this What you told http://www.mkyong.com/struts2/struts-2-dynamic-image-example/ – Dan Dec 17 '12 at 09:39

2 Answers2

0

You can not access directly anything else that your webapp from the browser (and even there some folders are protected). Imagine the security hole that it would be if you could.

You can write a "proxy" servlet/JSP that reads a file from your filesystem and returns it back to the browser, though. You may need to ensure proper access to your tomcat user, and give it access with selinux if you are using it.

But please, don't do that without a good reason and all the checks that you are only accessing the path that you intend to (so, check that nobody is passing you a file like ../../mySecrectAccountPasswords.txt).

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

You can store the image in any place accesible from your server app.

You must open the file and send the stream to the response using an especific servlet wich url you put in your jsp.

This is an example with Spring:

            File file = new File(...);
            Resource resource = new FileSystemResource( file );

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType( MediaType.IMAGE_JPEG );

            return new ResponseEntity<Resource>( resource, headers,
                    HttpStatus.OK );

Edited: You have a lot of examples searchin in Google for: "Streaming image to http response".

francadaval
  • 2,451
  • 3
  • 26
  • 36