Read a file form web-app

♀尐吖头ヾ 提交于 2020-01-12 18:46:03

问题


Inside a grails application, I need to upload a file under web-app/js, add a prefix, and put it in S3. I'm having trouble figuring out how to read the js file in a way that will work in development (/web-app/js) and production (/js). I'm doing this from inside a domain object.


回答1:


In your controllers, you can call :

def jsFolder = grailsAttributes.getApplicationContext().getResource("js/").getFile()

and then proceed with jsFolder. To determine the base directory of a running Grails application, use

String dir = applicationContent.getResource("/").getFile()

Getting the js path from a service is a little bit tricky:

You need to implement the ApplicationContextAware interface like this :

class MyService implements ApplicationContextAware {
ApplicationContext applicationContext

However, calling this code from a domain class is not a good idea (see this thread for some explanations) and I am not even sure if it's possible except from getting paths from manual configurations

Hope it helps.



来源:https://stackoverflow.com/questions/2573812/read-a-file-form-web-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!