我正在尝试实现一种将文件上传到特定本地目录的方法。
在我看来,我有这个:
<g:form action="upload" enctype="multipart/form-data" useToken="true">
<span class="button">
<input type="file" name="filepdf"/>
<input type="submit" class="upload" value="upload"/>
</span>
</g:form>
这是我的控制器动作:
def upload(){
def f =request.getFile('filepdf')
File fileDest = new File(grailsApplication.config.vote4.userGuides.userGuidesDirectory)
f.transferTo(fileDest)
}
这是我得到的错误:无法在null对象上调用方法transferTo()。 Stacktrace如下:
2016-11-21 18:52:24.559 [http-nio-8080-exec-6] ERROR org.grails.web.errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [POST] /backDoor/upload - parameters:
SYNCHRONIZER_TOKEN: ab3587c3-21b7-4618-b7c7-0a6db19867f0
SYNCHRONIZER_URI: /backDoor/retrieveCustomerDocumentation
Cannot invoke method transferTo() on null object. Stacktrace follows:
java.lang.reflect.InvocationTargetException: null
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53) ~[spring-security-core-3.0.0.M2.jar:na]
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62) ~[spring-security-core-3.0.0.M2.jar:na]
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53) ~[spring-security-core-3.0.0.M2.jar:na]
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62) ~[spring-security-core-3.0.0.M2.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_79]
Caused by: java.lang.NullPointerException: Cannot invoke method transferTo() on null object
at com.net4.vote4.control.backdoor.BackDoorController$$EQ3DVuLN.upload(BackDoorController.groovy:238) ~[na:na]
... 7 common frames omitted
这看起来我的表格没有正确提交。我错过了一些逻辑吗?
答案 0 :(得分:1)
将表单更改为 uploadForm
<g:uploadForm action="upload" enctype="multipart/form-data" useToken="true">
<span class="button">
<input type="file" name="filepdf"/>
<input type="submit" class="upload" value="upload"/>
</span>
</g:uploadForm>
现在您可以在上传操作中获取文件。
答案 1 :(得分:0)