我有一個 mysql db,我有一個名為 User 的表,其中包含一個名為 pct 的列,該列屬于 Blob 型別。
我正在使用 hibernate 執行本機選擇查詢,如下所示:
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;
}
我不能使用物體,因為它是一種通用方法,應該適合任何表,因此與特定表無關。基本上,當執行查詢并且對于 blob 型別的列 pct 時,將顯示以下值:
[B@1a270232
根據我的
是否可以將值 [B@1a270232 轉換為 base64,以便我可以在瀏覽器上顯示它?提前致謝
uj5u.com熱心網友回復:
怎么樣:
// ... once get hold of a byte array:
byte[] bytes = (byte[]) entry.getValue();
System.out.println(Base64Utils.encodeToString(bytes));
// ...
Javadoc:
- Spring-Base64Utils
uj5u.com熱心網友回復:
使用與您的確切要求相匹配的 apache IOUtils 的替代解決方案。
這是來自我的Github 倉庫。我在哪里擁有物體:
@Lob
private Byte[] userpicture;
獲取影像的控制器:
@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());
}
}
最后是顯示影像的視圖:
<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" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383648.html
下一篇:如何使用JPQL進行布爾切換
