Sachin J
Sachin J

Reputation: 2201

Spring MVC:How to get file path into Controller?

I have following folder structure:

ProjectFolder/images/some images

In the same folder

ProjectFolder/WEB-INF/classes/com/xyz/here is java file of controller.

How can I get the image path in the controller?

Please, help. Thanks :)

Upvotes: 6

Views: 20149

Answers (2)

JOHND
JOHND

Reputation: 2777

You can store your image path in properties file.

store that property file in your classpath.

now access that property in your controller class like this:

Properties properties = new Properties();
/*to access your filename.properties file */
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));

String  sServerLocation = properties.getProperty("server.upload.docs.path");

note that you should use escape charater in your property file like :

server.upload.docs.path=D:\\JDIS3\\DOCS\\

Upvotes: 0

Moinul Hossain
Moinul Hossain

Reputation: 2206

If its a web context may be something like this could help

InputStream is = null ;
is = request.getSession().getServletContext().getResourceAsStream("/images/someimage.jpg");

or may be something like this:

InputStream is = null ;
String realPath  = request.getSession().getServletContext().getRealPath("/images/someimage.jpg");
is = new FileInputStream(realPath);

Upvotes: 10

Related Questions