1

Instead of having a path variable (i.e. for accessing images) in different parts of JAVA Spring Controllers is there a way to define a variable in config file and then use it simply through such variable:

Config file:
String imageFolder = "D:\\Projects\\project_name\\images\\";

Spring Controller:
File outputFile = new File(imageFolder + img_name + "." + img_ext);
ImageIO.write(image, img_ext, outputFile);

Thaks in advance.

Dancyg
  • 139
  • 1
  • 15
  • where is spring controller? What above code does? – SMA Jul 11 '16 at 12:26
  • I think you want to map image folder that is outside your project directory. [This](http://stackoverflow.com/questions/1483063/how-to-handle-static-content-in-spring-mvc) post might help you out. – Prabhat Jul 11 '16 at 12:32
  • Spring controller accepts images in base64 format and writes it to the external folder. I need to have String variable with path to the folder and use it in different places. is it possible? – Dancyg Jul 11 '16 at 12:33

1 Answers1

0

Own solution (added in the bottom):

web.xml:

<context-param>
    <param-name>imageFolder</param-name>
    <param-value>D:\Projects\project_name\src\main\webapp\resource\images\</param-value>
</context-param>

Controller:

@RestController
public class nameController {    

    @Resource
    private ServletContext servletContext;

    @RequestMapping(value = "/getImage/{img_name:.+}")
    public byte[] getImage(@PathVariable String img_name) throws InternalError  {
    byte[] data;

     try {
         String imageFolder = servletContext.getInitParameter("imageFolder");

         String realpath = imageFolder + img_name;
         Path path = Paths.get(realpath);
         data = Files.readAllBytes(path);
     }catch (Exception e){
         data = null;
     }
    return data;
   }
}
Dancyg
  • 139
  • 1
  • 15