如何使用spring mvc从mysql数据库显示图像 [英] How to display image from mysql database using spring mvc
问题描述
我使用spring mvc为项目类项目(itemId,itemName,itemPrice,itemContent,itemImage)存储BLOB类型图像在我的数据库中,但是当我试图显示它在我的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页面与图像一起显示项目属性。
CategoryId:
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>
如何正确显示存储在数据库中的图像。我猜我做错了通过显示图像ilke这在jsp。
但是如何在jsp中显示图像。
How can i properly display the image which is stored in database.I guess i'm doing it wrong by displaying image ilke 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 i中执行此操作
and in the jsp i did this
<img src="/Project1/myImage/imageDisplay?id=${item.itemId}"/>
图片已成功显示。
这篇关于如何使用spring mvc从mysql数据库显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!