2

I am creating a Spring MVC Web Application in which I am uploading images from user and saving them at project folder in my drive (local laptop) and I am able to retrieve them successfully.

To save the images I am using the code:

MultipartFile productImage = product.getProductImage();
               int Id= productService.addProduct(product);
        path= Paths.get("C:/Users/oms/images/"+Id+".jpg");
        //System.out.println(path.toString());
        if(productImage!=null && !productImage.isEmpty()){
            try {
                productImage.transferTo(new File(path.toString()));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Product Image Saving Failed ",e);

            }
        }

Image is getting saved successfully.Now to retrieve them I am using the code:

<mvc:resources mapping="/images/**" location="file:///C:/Users/oms/images/" />

I am able to retrieve them successfully.

But this application works fine in local but If I deploy the war file in cloud It won't work because there is no C drive and no users. I tried using the relative path to save the images in project folder but all failed.

I tried:

 System.out.println("request.getContextPath()"+request.getContextPath());
  System.out.println("request.getPathInfo()"+request.getPathInfo());
 System.out.println("request.getPathTranslated()"+request.getPathTranslated());
 System.out.println("request.getServletPath()"+request.getServletPath());
 System.out.println("servletContext.getRealPath()"+servletContext.getRealPath("/"));

and all null except the getRealPAth().

servletContext.getRealPath()C:\Users\apache-tomcat-8.0.24\webapps\ROOT\

Obviously I can't save them in webapps folder because after restarting the server all images will be gone(deleted). How can I get the relative path to save images in the project folder so that when I am deploying this project in cloud it works fine?

halfer
  • 19,824
  • 17
  • 99
  • 186
shiva
  • 730
  • 8
  • 25

1 Answers1

0

From a JSP you can't retrieve a file from the file system of your server, so you have two options:

  • Store the image in a folder that you can access with a url (for example inside an Apache htdocs folder)

  • Sotre de image into the server's file system and call a servlet to obtain it.

  • can you please write the code to save image in apache htdocs folder or how can i save it in server's file system ? Because I have no idea. But ya in my local system it works perfect . But I am using the full path that's the problem .... so do you have any solution please share :) – shiva Jun 23 '16 at 12:13
  • Well to use an Apache server (or any other) you must first install and configure it. Probably what you need is use a servlet to get the content of the image. There are tons of examples of how to do it. For example: http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page – Marco A. Hernandez Jun 23 '16 at 12:22
  • I am not saving images into the database . I am using them as a file system. Well I will look for example. Just I wanna know can i save them in server folder ? because the default path is webapps root folder and I can't save images into the webapps folder :( – shiva Jun 23 '16 at 12:25
  • First of all you need a path in the server to store the data. If you are not the owner of the server then ask for one. Don't use a letter like C:/ or D:/ unless your server provider tells you to do. Probably your path will be something like //. You should use this path to write and to read your images – Marco A. Hernandez Jun 23 '16 at 14:06