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:圖像正確保存在數據庫,我可以看到它,但我不能表現出來對我的普惠制。
有什麼想法?
非常感謝很多傢伙,
請在評論中添加此類建議。 –
它的工作原理,謝謝! – user1286733