【问题标题】:GRAILS- Image corrupt or truncatedGRAILS - 图像损坏或截断
【发布时间】:2013-02-05 14:31:32
【问题描述】:

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

我的域类 User 有一个属性 photo 定义为 byte[]

public class User {

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

user.hbm.xml 映射为:

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

此列是 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>

配置文件控制器:

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();
}

仅供参考:图片已正确保存在数据库中,我可以看到它,但我无法在我的 gsp 上显示它。

有什么想法吗?

非常感谢各位,

【问题讨论】:

    标签: image hibernate grails blob truncated


    【解决方案1】:

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

    【讨论】:

    • 请在 cmets 中添加此类建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2016-05-12
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多