1

I'm currently use Intellij idea for grails project.

I want to upload, user profile picture to project directory. e.g. E:\MyProject\userUploads

currently i'm trying to get path using following code

def filePath = request.getSession().getServletContext().getRealPath("/")

But when i print the filePath i get:

C:\Users\Rahul.Mahadik\AppData\Local\Temp\tomcat-docbase.2236924879274963579.8080\

Also i was tried "servletContext.getRealPath("/")" this also to get path of my current directory but getting same path like above

Thanks

4

2 回答 2

0

You should understand the flow of building and running the web application in Grails.

When you start run-app command, Grails compiles your code and creates war file - this is a web application archive. In order to run this war file you need some web server. Grails has its embedded Tomcat server, so it gets the war file and deploys it to the Tomcat.
Tomcat has its own folder where web application archive is deployed. So the path to your application is the path to some folder where Tomcat server is running your application. That's why you see

C:\Users\Rahul.Mahadik\AppData\Local\Temp\tomcat-docbase.2236924879274963579.8080\

Obviously, Tomcat has no idea where your project is located. So you can't get this path when your application is running.

It's a common practice to make the path for file storage configurable. You can get the idea how to use external config file in this topic: How to use external .groovy config file in Grails 3

于 2017-04-07T14:23:53.997 回答
0

Solved my problem by adding following in application.yml

server.contextPath: /

OR

If i run app with grails -Dapp.context=/ run-app

于 2018-03-28T19:29:46.340 回答