Grails上传图像
我正试图上传一张图片,并将其保存在数据库中。 如果执行以下操作,则会出现此错误: 无法转换类型的属性值 org.springframework.web.multipart.commons.commons多部分文件 属性picture1的必需类型java.lang.Byte[];嵌套异常 is java.lang.IllegalArgumentException:无法转换类型的值 [org.springframework.web.multipart.commons.commons multipartfile]到 属性picture1[0]所需的类型[java.lang.Byte]: 属性编辑器 [org.springframework.beans.propertyEdit.CustomNumberEditor] 返回的值不正确 如果我这样做:Grails上传图像,grails,file-upload,Grails,File Upload,我正试图上传一张图片,并将其保存在数据库中。 如果执行以下操作,则会出现此错误: 无法转换类型的属性值 org.springframework.web.multipart.commons.commons多部分文件 属性picture1的必需类型java.lang.Byte[];嵌套异常 is java.lang.IllegalArgumentException:无法转换类型的值 [org.springframework.web.multipart.commons.commons multipar
if(request instanceof MultipartHttpServletRequest){
MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("picture1");
}
我得到这个错误:
正在执行控制器[com.testapp.RequestController]的操作[save]
导致异常:无法强制转换对象的
org.springframework.web.multipart.commons。CommonsMultipartFile@34ae1f02'
有课
“org.springframework.web.multipart.commons.commons multipartfile”到
类“java.lang.Byte”
我该怎么做才能让它工作
域
普惠制:
请不要介意意大利面代码。我只是想了解一些基本概念。我稍后会清理它
简化示例:
def save = {
def requestInstance = new Request(params)
def requestNumberInstance = new RequestNumber()
if(requestInstance.validate() && requestInstance.save(flush: true)){
println "Saved successfully with ${requestInstance.picture1.length} bytes"
}
else {
println "Save failed"
}
我猜你的打字打错了 如果您只需执行以下操作,会发生什么情况:
def f = request.getFile('myFile')
如手册所述。如果您想要强类型,就我所记得的而言,它应该是MultiPartfile,而不是commonmultipartfile(您可以从request对象得到它)
这是您正在使用的界面:
对于您来说,相关的方法应该是getBytes()
编辑已编辑的问题:
正如我所说的,如果您想要getBytes(),那么您试图将一个多部分文件推入一个字节数组,这是行不通的
requestInstance.picture=upPic.getBytes()
你应该没问题。问题编辑后更新
错误可能是由以下原因引起的:
def upPic1 = request.getFile('picture1')
...
requestInstance.picture1 = upPic1
request.getFile()
正在返回一个多部分文件,您试图将其分配给字节[]
字段。考虑到我下面的小例子,你甚至不需要做这个作业。def requestInstance=new Request(params)
将自动绑定字节[]
上传的文件自动绑定到
字节[]
字段。下面是一个工作示例:
域:grails-app/Domain/my/Example.groovy
package my
class Example {
byte[] file
}
package my
class ExampleController {
def create = { }
def save = {
def example = new Example(params)
if(example.validate() && example.save(flush: true)) {
println "Saved successfully with ${example.file.length} bytes"
} else {
println "Save failed"
}
redirect(action: 'create')
}
}
控制器:grails应用程序/controllers/my/ExampleController.groovy
package my
class Example {
byte[] file
}
package my
class ExampleController {
def create = { }
def save = {
def example = new Example(params)
if(example.validate() && example.save(flush: true)) {
println "Saved successfully with ${example.file.length} bytes"
} else {
println "Save failed"
}
redirect(action: 'create')
}
}
GSP:grails应用程序/views/example/create.GSP
<!DOCTYPE html>
<html>
<body>
<g:uploadForm action="save">
<input type="file" name="file"/>
<g:submitButton name="submit"/>
</g:uploadForm>
</body>
</html>
建议
- 尝试使用Grails数据绑定保存文件内容
- 如果您使用的是普通的`,请确保您的表单是
或具有enctype=“多部分/表单数据”
- 确保使用域构造函数、
或domain.properties
绑定参数bindData()
byte [] picture1
byte [] picture2
byte [] picture3
并添加了这些映射:
static mapping = {
picture1 column:"picture1", sqlType: "blob"
picture2 column:"picture2", sqlType: "blob"
picture3 column:"picture3", sqlType: "blob"
}
提供相关的控制器和域代码也会有所帮助。谢谢更新问题。跟进:您的控制器如何将请求参数绑定到域?感谢您如此快速地回答。请参阅我的更新答案:您的最新问题更新。我已经尝试了您的建议,但仍然不起作用。我仍然收到错误:未能将org.springframework.web.multipart.commons.commons multipartfile类型的属性值转换为属性picture1所需的java.lang.Byte[]类型;嵌套异常为java.lang.IllegalArgumentException:无法将[org.springframework.web.multipart.commons.commons multipartfile]类型的值转换为属性picture1[0]:PropertyEditor[org.springframework.beans.propertyeditors.customnumberditor]所需的类型[java.lang.Byte]返回不适当value@drago-你到底试了什么?我想你删除了我在更新中引用的两行?我只是想澄清一下。是的,我删除了这两行,并尝试了Oliver Tynes的建议。两者都不起作用。我开始意识到我忽略了一些基本的东西。但是什么?我以后得看一看;不过你说得对,有些事情似乎不对劲。同时,也许可以将代码缩减到再现问题的最小可能情况。这可能有助于您的调试。快速建议-您可以尝试将域类型从
Byte[]
更改为Byte[]
,看看是否有帮助。不过,我不确定它会不会。值得一试。这就是我所做的:def picture1=request.getFile('picture1')requestInstance.picture1=picture1.getBytes()我得到的结果是:无法将属性picture1的类型org.springframework.web.multipart.commons.commons multipartfile的属性值转换为所需的类型java.lang.Byte[];嵌套异常为java.lang.IllegalArgumentException:无法将[org.springframework.web.multipart.commons.commons multipartfile]类型的值转换为属性picture1[0]:PropertyEditor[org.springframework.beans.propertyeditors.customnumberditor]所需的类型[java.lang.Byte]返回了不适当的值得到的行引用是什么(将代码也粘贴到该行)?这个错误看起来不属于你在上面的评论中发布的代码示例。我知道你的意思,但这是正确的代码。我发布了有效的解决方案。谢谢你的帮助。事实上,我没有注意到你用的是Byte而不是Byte,对此我很抱歉。
byte [] picture1
byte [] picture2
byte [] picture3
static mapping = {
picture1 column:"picture1", sqlType: "blob"
picture2 column:"picture2", sqlType: "blob"
picture3 column:"picture3", sqlType: "blob"
}