gpt4 book ai didi

grails - request.getFile()返回空对象

转载 作者:行者123 更新时间:2023-12-02 14:40:10 27 4
gpt4 key购买 nike

我正在尝试实现一种将文件上传到特定本地目录的方法。
我认为:

<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>

这是在我的 Controller 操作中:
 def upload(){
def f =request.getFile('filepdf')
File fileDest = new File(grailsApplication.config.vote4.userGuides.userGuidesDirectory)
f.transferTo(fileDest)
}

这是我得到的错误:无法在空对象上调用方法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

看来我的表格未正确提交。我是否缺少执行此操作的逻辑?

最佳答案

表单类型更改为 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>

现在您可以在上传操作中获取文件了。

关于grails - request.getFile()返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com