在数据库中存储图片的地址,在jsp中可以取出地址,怎么显示为图片
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
2个回答
展开全部
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddForm addForm = (AddForm) form;
ActionErrors errors = new ActionErrors();
try {
// 获得要上传文件
FormFile file = addForm.getFile();
System.out.println(file.getContentType());
if (!"image/pjpeg".equals(file.getContentType())) {
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"你选择的文件类型有误", false));
this.addErrors(request, errors);
return mapping.findForward("error");
}
// 获得上传文件名
String filename = file.getFileName();
// 获得新的文件名称
String newfilename = DataDefine.getDateId() + "."
+ filename.substring(filename.lastIndexOf("."));
String imagespath = "images" + "/" + newfilename;
// 获得服务器上传目录
String dir = this.getServlet().getServletContext().getRealPath(
"images");
// 获得输入流
InputStream in = file.getInputStream();
// 定义文件输入流
OutputStream fileout = new FileOutputStream(dir + File.separator
+ newfilename);
int c = 0;
byte[] buffer = new byte[1024];
while ((c = in.read(buffer, 0, 1024)) != -1) {
fileout.write(buffer, 0, c);
}
file.destroy();
// 数据库记录信息
Product pro = new Product();
pro.setProductname(addForm.getProductname());
pro.setImagespath(imagespath);
biz.saveProduct(pro);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
JSP:
XML/HTML code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1.添加
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for AddForm form</title>
</head>
<body>
<center>
<html:form action="/add" enctype="multipart/form-data">
产品名称 : <html:text property="productname"/><html:errors property="productname"/><p/>
产品的图片:<html:file property="file" /><p/>
<html:submit value="添加" />
</html:form>
</center>
</body>
</html>
2:显示
<table border="1" align="center">
<c:forEach items="${query.list}" var="product">
<tr>
<td>${product.productname}</td>
<td><img src="${product.imagespath}" /></td>
</tr>
</c:forEach>
</table>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddForm addForm = (AddForm) form;
ActionErrors errors = new ActionErrors();
try {
// 获得要上传文件
FormFile file = addForm.getFile();
System.out.println(file.getContentType());
if (!"image/pjpeg".equals(file.getContentType())) {
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"你选择的文件类型有误", false));
this.addErrors(request, errors);
return mapping.findForward("error");
}
// 获得上传文件名
String filename = file.getFileName();
// 获得新的文件名称
String newfilename = DataDefine.getDateId() + "."
+ filename.substring(filename.lastIndexOf("."));
String imagespath = "images" + "/" + newfilename;
// 获得服务器上传目录
String dir = this.getServlet().getServletContext().getRealPath(
"images");
// 获得输入流
InputStream in = file.getInputStream();
// 定义文件输入流
OutputStream fileout = new FileOutputStream(dir + File.separator
+ newfilename);
int c = 0;
byte[] buffer = new byte[1024];
while ((c = in.read(buffer, 0, 1024)) != -1) {
fileout.write(buffer, 0, c);
}
file.destroy();
// 数据库记录信息
Product pro = new Product();
pro.setProductname(addForm.getProductname());
pro.setImagespath(imagespath);
biz.saveProduct(pro);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
JSP:
XML/HTML code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1.添加
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for AddForm form</title>
</head>
<body>
<center>
<html:form action="/add" enctype="multipart/form-data">
产品名称 : <html:text property="productname"/><html:errors property="productname"/><p/>
产品的图片:<html:file property="file" /><p/>
<html:submit value="添加" />
</html:form>
</center>
</body>
</html>
2:显示
<table border="1" align="center">
<c:forEach items="${query.list}" var="product">
<tr>
<td>${product.productname}</td>
<td><img src="${product.imagespath}" /></td>
</tr>
</c:forEach>
</table>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<img src='<%=rs.getString("path")%>'/>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询