如何使用spring mvc显示mysql数据库中的图像 [英] How to display image from mysql database using spring mvc
问题描述
我使用 Spring MVC 将 BLOB 类型图像存储在 MySQL 数据库中,用于项目类 Item(itemId、itemName、itemPrice、itemContent、itemImage).我成功地将图像存储在数据库中,但是当我尝试在我的 jsp 中显示它时,它显示的是二进制文件,如 [B@7fb0c025.
I am storing BLOB type image in MySQL database using Spring MVC for item class Item (itemId, itemName, itemPrice, itemContent, itemImage). I successfully stored image in database but when I'm trying to display it in my jsp, it is showing something binary like [B@7fb0c025.
如何在 JSP 中显示正确的图像(图像存储在 MySQL 数据库表中)
How can I display proper image in JSP (image is stored in MySQL database table)
我的模型类:
@Entity
@Table(name="item")
public class Item {
@Id
@Column(name="ItemId")
@GeneratedValue
private Integer itemId;
@Column(name="ItemName")
private String itemName;
@Column(name="ItemContent")
private String itemContent;
/*
@Column(name="ItemImage")
private ByteArray ItemImage;
*/
@Column(name="ItemPrice")
private int itemPrice;
@Column(name="ItemImage")
private byte[] itemImage;
addItem.jsp"添加项目属性以及数据库中的图像.
"addItem.jsp" to add item attributes along with the image in database.
<form:form modelAttribute="itemAttribute" enctype="multipart/form-data" method="POST" action="${Url}">
<table>
<tr>
<td><form:label path="itemId"></form:label></td>
<td><form:input path="itemId" type="hidden"/></td>
</tr>
<tr>
<td><form:label path="itemName">ItemName:</form:label></td>
<td><form:input path="itemName"/></td>
</tr>
<tr>
<td><form:label path="itemPrice">ItemPrice:</form:label></td>
<td><form:input path="itemPrice"/></td>
</tr>
<tr>
<td><form:label path="itemContent">ItemContent:</form:label>
<td><form:input path="itemContent"/>
</tr>
<tr>
<form:label for="itemImage" path="itemImage">itemImage:</form:label>
<form:input path="itemImage" type="file" />
</tr>
</table>
<input type="submit" value="Save" />
</form:form>
显示项目属性和图像的 JSP 页面.类别编号:
The JSP page to display item attributes along with the image. CategoryId:
<tr>
<td><form:label path="categoryName">CategoryName:</form:label></td>
<td><form:input path="categoryName"/></td>
</tr>
</table>
<input type="submit" value="Save" />
<table width: 100%; text-align:center">
<tr>
<th>ItemId</th>
<th>ItemName</th>
<th>ItemPrice</th>
<th>ItemFeatures</th>
<th>Edit</th>
<th>Delete</th>
<th>ItemImage</th>
</tr>
<tbody>
<c:forEach items="${categoryAttribute.item}" var="item">
<tr>
<c:url var="editCUrl" value="/item/edit?bid=${categoryAttribute.categoryId}&cid=${item.itemId}" />
<c:url var="deleteCUrl" value="/item/delete?id=${item.itemId}" />
<td><c:out value="${item.itemId}" /></td>
<td><c:out value="${item.itemName}"/></td>
<td><c:out value="${item.itemPrice}"/></td>
<td><c:out value="${item.itemContent}"/></td>
<td><a href="${editCUrl}">EditItem</a></td>
<td><a href="${deleteCUrl}">DeleteItem</a></td>
<td><c:out value="${item.itemImage}"/></td>
</tr>
</c:forEach>
如何正确显示存储在数据库中的图像?我想我在 JSP 中显示这样的图像是错误的.但是如何在 JSP 中显示这里的图像?
How can I properly display the image which is stored in the database? I guess I'm doing it wrong by displaying image like this in JSP. But how can I display the image here in JSP?
推荐答案
我终于可以在我的 jsp 上显示图像了.我做了什么.
I'm finally able to display the image on my jsp. what i did.
我单独创建了一个这样的控制器.
I separately created a controller like this.
@Controller
@RequestMapping("/myImage")
public class ImageController {
@Resource(name="categoryService")
private CategoryService categoryService;
@Resource(name="itemService")
private ItemService itemService;
@RequestMapping(value = "/imageDisplay", method = RequestMethod.GET)
public void showImage(@RequestParam("id") Integer itemId, HttpServletResponse response,HttpServletRequest request)
throws ServletException, IOException{
Item item = itemService.get(itemId);
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
response.getOutputStream().write(item.getItemImage());
response.getOutputStream().close();
在jsp中我做了这个
<img src="/Project1/myImage/imageDisplay?id=${item.itemId}"/>
图像显示成功.
这篇关于如何使用spring mvc显示mysql数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!