如何使用 Spring 将 blob 转换为图像
How to convert blob to image with Spring
我有一个 mysql 数据库和一个名为 User 的 table,它包含一个名为 pct 的列,属于 Blob 类型。
我正在使用休眠执行本机 select 查询,如下所示:
public List<Map<String, Object>> queryExtraction(String sql, QWSInputParam[] qwsInputParams) {
sql = "SELECT user.name,user.pct FROM user user WHERE user.id = user.id and user.id in :PARAM0";
Query query = getSession().createSQLQuery(sql);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
for (int i = 0; i < qwsInputParams.length; i++) {
LOGGER.info("PARAM" + i + ": " + Arrays.toString(qwsInputParams[i].getValues()));
query.setParameterList("PARAM" + i, qwsInputParams[i].getValues());
}
//LOGGER.info("Query extraction: " + query.toString());
//query.setTimeout(QUERY_TIME_OUT);
List<Map<String, Object>> list = query.list();
Object value = null;
for (Map<String, Object> map : list) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
value = entry.getValue();
System.out.println("0 " + entry.getValue());
}
}
return list;
}
我不能使用实体,因为它是一个通用方法,应该满足任何 table 的需要,因此与特定的 table 无关。
基本上,当执行查询时,对于 blob 类型的列 pct,将显示以下值:
[B@1a270232
基于 post 我理解这是一个 JNI 类型签名。
table中的pct值如下图:
是否可以将值 [B@1a270232] 转换为 base64,以便我可以在浏览器上显示它?
提前致谢
怎么样:
// ... once get hold of a byte array:
byte[] bytes = (byte[]) entry.getValue();
System.out.println(Base64Utils.encodeToString(bytes));
// ...
Javadoc:
使用 apache IOUtils 的替代解决方案符合您的确切要求。
这是来自我的 Github repo. Where I have the the Entity 有:
@Lob
private Byte[] userpicture;
The controller 获取要查看的图像:
@GetMapping("appUser/{id}/appUserimage")
public void renderImageFromDB(@PathVariable String id, HttpServletResponse response) throws IOException {
AppUserCommand appUserCommand = appUserService.findCommandById(Long.valueOf(id));
if (appUserCommand.getUserpicture() != null) {
byte[] byteArray = new byte[appUserCommand.getUserpicture().length];
int i = 0;
for (Byte wrappedByte : appUserCommand.getUserpicture()){
byteArray[i++] = wrappedByte; //auto unboxing
}
response.setContentType("image/jpeg");
InputStream is = new ByteArrayInputStream(byteArray);
IOUtils.copy(is, response.getOutputStream());
}
}
最后显示图片的 view:
<div th:if="${appUser.userpicture == null}">
<img th:src="@{/images/defaultuser.jpg}" width="40" height="40" />
<a href="#" th:href="@{'/appUser/' + ${appUser.id} + '/image'}" role="button">Upload</a> </div>
<div th:if="${appUser.userpicture != null}"><img th:src="@{'/appUser/' + ${appUser.id} + '/appUserimage'}" width="40" height="40" />
我有一个 mysql 数据库和一个名为 User 的 table,它包含一个名为 pct 的列,属于 Blob 类型。
我正在使用休眠执行本机 select 查询,如下所示:
public List<Map<String, Object>> queryExtraction(String sql, QWSInputParam[] qwsInputParams) {
sql = "SELECT user.name,user.pct FROM user user WHERE user.id = user.id and user.id in :PARAM0";
Query query = getSession().createSQLQuery(sql);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
for (int i = 0; i < qwsInputParams.length; i++) {
LOGGER.info("PARAM" + i + ": " + Arrays.toString(qwsInputParams[i].getValues()));
query.setParameterList("PARAM" + i, qwsInputParams[i].getValues());
}
//LOGGER.info("Query extraction: " + query.toString());
//query.setTimeout(QUERY_TIME_OUT);
List<Map<String, Object>> list = query.list();
Object value = null;
for (Map<String, Object> map : list) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
value = entry.getValue();
System.out.println("0 " + entry.getValue());
}
}
return list;
}
我不能使用实体,因为它是一个通用方法,应该满足任何 table 的需要,因此与特定的 table 无关。 基本上,当执行查询时,对于 blob 类型的列 pct,将显示以下值:
[B@1a270232
基于 post 我理解这是一个 JNI 类型签名。
table中的pct值如下图:
是否可以将值 [B@1a270232] 转换为 base64,以便我可以在浏览器上显示它? 提前致谢
怎么样:
// ... once get hold of a byte array:
byte[] bytes = (byte[]) entry.getValue();
System.out.println(Base64Utils.encodeToString(bytes));
// ...
Javadoc:
使用 apache IOUtils 的替代解决方案符合您的确切要求。
这是来自我的 Github repo. Where I have the the Entity 有:
@Lob
private Byte[] userpicture;
The controller 获取要查看的图像:
@GetMapping("appUser/{id}/appUserimage")
public void renderImageFromDB(@PathVariable String id, HttpServletResponse response) throws IOException {
AppUserCommand appUserCommand = appUserService.findCommandById(Long.valueOf(id));
if (appUserCommand.getUserpicture() != null) {
byte[] byteArray = new byte[appUserCommand.getUserpicture().length];
int i = 0;
for (Byte wrappedByte : appUserCommand.getUserpicture()){
byteArray[i++] = wrappedByte; //auto unboxing
}
response.setContentType("image/jpeg");
InputStream is = new ByteArrayInputStream(byteArray);
IOUtils.copy(is, response.getOutputStream());
}
}
最后显示图片的 view:
<div th:if="${appUser.userpicture == null}">
<img th:src="@{/images/defaultuser.jpg}" width="40" height="40" />
<a href="#" th:href="@{'/appUser/' + ${appUser.id} + '/image'}" role="button">Upload</a> </div>
<div th:if="${appUser.userpicture != null}"><img th:src="@{'/appUser/' + ${appUser.id} + '/appUserimage'}" width="40" height="40" />