0

データベースに保存されている画像を表示しようとすると、FFコンソールでこのエラー「画像が破損または切り捨てられました」が表示されます。GrailsとHibernateを使用しています。gspページにユーザーのプロフィール写真を表示したいだけです。

私のドメインクラスUserには、byte[]として定義された属性photoがあります

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>

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

参考:画像はDBに正しく保存されています。表示できますが、gspに表示できません。

何か案が?

どうもありがとう、

4

1 に答える 1

0

マッピングでブロブタイプを使用してください:)。

于 2013-02-06T13:32:59.100 に答える