我的域类用户将属性照片定义为byte []
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();
}
仅供参考:图片正确保存在DB上,我可以看到,但我无法在我的gsp上显示。
有什么想法吗?
非常感谢,
答案 0 :(得分:0)
在映射中使用blob类型:)。