2013-02-05 59 views
0

当我尝试显示存储在我的数据库中的图像时,即时在FF控制台上发生此错误“image corrupt or truncated”。我正在使用Grails和Hibernate。 我只想在我的gsp页面中显示用户的个人资料图片。GRAILS-图像损坏或截断

我的域类用户具有被定义为字节的属性相片[]

public class User { 

private String userId; 
private byte[] photo; 
. 
. 
. 

的user.hbm.xml映射为:

< property name="photo" type="binary" column="PHOTO" not-null="false" /> 

这collumn是DB一个BLOB。

我GSP:

< div id="user-view-thumbnail-container"> 
    <fieldset> 
     <legend>Photo Upload</legend> 
     <g:form action="uploadPhoto" method="post" enctype="multipart/form-data"> 
     <label for="photo">Photo</label> 
     <input type="file" name="photo" id="photo" /> 
     <input type="submit" class="buttons" value="Upload" /> 
     </g:form> 
    </fieldset> 
    <g:if test="${user.photo}"> 
    <img alt="User Photo" src="${createLink(controller:'profile', action:'profile_image', id:user.userId)}" /> 
    </g:if> 
</div> 

ProfileController可:

def uploadPhoto = {  
    def user = userService.getUser(authenticateService.principal()) 
    def f = request.getFile('photo') 

    user.setPhoto(f.getBytes()) 

    if (!user.save()) { 
     //doesn´t matter now 
     return; 
    } 

    redirect(action: 'index', model:[user: user]) 
} 

def profile_image = { 
    def user = userService.getUser(params.id) 

    if (!user) { 
     response.sendError(404) 
     return; 
    } 
    response.setContentType(user.photo.contentType())//'image/png', 'image/jpeg', 'image/gif' 
    response.setContentLength(user.photo.size()) 
    OutputStream out = response.getOutputStream(); 
    out.write(user.photo); 
    out.close(); 
} 

FYI:图像正确保存在数据库,我可以看到它,但我不能表现出来对我的普惠制。

有什么想法?

非常感谢很多家伙,

回答

0

在映射中使用blob类型:)。

+1

请在评论中添加此类建议。 –

+0

它的工作原理,谢谢! – user1286733