그루비에서 클래스 캐스트 예외
-
16-09-2019 - |
문제
성배에 그루비를 사용하여 이미지를 업로드하고 싶습니다. 내 GSP 페이지는 다음과 같습니다 (원본의 단순화 된 버전을 보여줍니다)
<g:form controller="post" action="save" enctype="multipart/form-data">
My picture <input type="file" name="myPicture" />
<g:submitButton name="submit" value="Save"/>
</g:form>
내 도메인 클래스는 다음과 같습니다.
class Post {
byte[] myPicture
static mapping = {
myPicture type: "blob"
}
이 매핑이 필요하지 않으면 MySQL이 이미지에 맞게 작은 작은 블로브를 만듭니다.
static constraints = {
myPicture(nullable:false)
}
}
컨트롤러에는 Save라는 조치가 있습니다.
def save = {
def post = loadPost(params.id)
post.properties = params
if(post.save()) {
print "hallo world"
redirect(action:'list', params:params)
} else {
render(view:'edit', model:[post:post])
}
}
DB에서 이미지를 저장하려고 할 때 예외가 발생합니다.
2009-04-27 18:16:07,319 [20806951@qtp0-0] ERROR errors.GrailsExceptionResolver - java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
org.codehaus.groovy.runtime.invokerinvocationexception : java.lang.classcastexception : [b는 java.sql.blob에 캐스트 할 수 없습니다.
힌트 왜이게 힌트?
BTW, 나는 튜토리얼에서 이미지가 문자열로 처리되었지만 작동하지 않았다는 것을 보았습니다.
해결책
이 방법을 시도하십시오 :
def save = {
def post = loadPost(params.id)
def f = request.getFile('myPicture')
post.myPicture = f.getBytes()
post.pictureType = f.getContentType()
if(post.save()) {
다른 팁
나는 nabble에서 비슷한 질문을 발견했다.
http://www.nabble.com/mysql-and-blobs-td16116885.html
두 가지 가능한 솔루션이 제안됩니다.
- Blob 속성의 제약 조건을 큰 최대 크기로 변경하여 "Tinyblob"을 사용하지 못하게하십시오.
- 속성 유형 선언에 바이트 [] 대신 최대 절전 모드 구현을 사용하십시오. 이를 위해서는 직접 할당 대신 데이터를 블로브로 스트리밍해야하지만 위의 게시물은 코드를 제공합니다.
Spring 's를 사용해 볼 수 있습니까? 멀티 파트 파일 당신 안에 loadPost()
방법?
다음은 다음과 같습니다 문서:
def upload = {
def f = request.getFile('myFile')
if(!f.empty) {
f.transferTo( new File('/some/local/dir/myfile.txt') )
response.sendError(200,'Done');
}
else {
flash.message = 'file cannot be empty'
render(view:'uploadForm')
}
}
나는 당신이 접근 할 수 있다고 생각합니다 f.bytes
곧장.
제휴하지 않습니다 StackOverflow