页面代码:
<form action="deleteGoodsServlet">
<table>
<tr>
<td>
选择
</td>
<td>
商品名称
</td>
<td>
商品 介绍
</td>
<td>
商品价格
</td>
<td>
实物图片
</td>
</tr>
<c:forEach var="goods" items="${goodslist}">
<tr>
<td>
<input type="checkbox" name="ids" value="${goods.id}">
</td>
<td>
<c:out value="${goods.name}" />
</td>
<td>
<c:out value="${goods.description}" />
</td>
<td>
<c:out value="${goods.price}" />
</td>
<td>
<img src="${goods.imagepath}" width="100" height="80">
</td>
<td>
<a href="editGoodsServlet?id=${goods.id}" />修改</a>
</td>
</tr>
</c:forEach>
<tr>
<td>
<input type="submit" value="删除">
</td>
<td>
<a href="filter/addGoods.jsp">添加书籍</a>
</td>
</tr>
</table>
servlet代码:
SmartUpload suplod = new SmartUpload();
suplod.initialize(this.getServletConfig(),request, response);
suplod.setMaxFileSize(1024*768);
suplod.service(request,response);
String imgename=null;
try {
suplod.upload();
imgename=suplod.getFiles().getFile(0).getFileName();
suplod.save("D:/image");
} catch (SmartUploadException e) {
e.printStackTrace();
}
String imagepath="D:/image"+imgename;
String name=suplod.getRequest().getParameter("name");
String description=suplod.getRequest().getParameter("description");
double price=Double.parseDouble(suplod.getRequest().getParameter("price"));
new GoodsDao().addGoods(name,description,price,imagepath);
request.getRequestDispatcher("goodsServlet").forward(request, response);
<img src='${pageContext.request.contextPath }/${goods.imagepath}' />
这个用的是绝对路径
这个和单双引号有点关系,还与文件的扩展名的大小写有关,路径最好用相对路径,路径与名字不要有中文出现
还有一种就是数据库里存图片的二进制信息,你自己在还原为图片