文章来源:http://blog.csdn.net/yuyuyuyuy/article/details/6298753#comments
这两天研究了一下图片存储在mysql数据库中,并显示在jsp页面上。
我创建的数据库只有一个表image(id int,image blob);
<hibernate-mapping>
<class name="entity.Images" table="image">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="image" type="blob" column="image"></property>
<!-- 当实体类中属性与数据库一致时,可省略type和column -->
</class>
</hibernate-mapping>
上传文件页面:upImages.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>上传照片</title>
</head>
<body>
<s:form action="ttt/UploadSubmit" enctype="multipart/form-data">
<s:file name="upload" label="请选择要上传的照片" /><!-- 似乎必须为upload -->
<s:submit value="上传" />
</s:form>
</body>
</html>
struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ttt" namespace="/" extends="struts-default">
<action name="UploadSubmit" class="action.MyUpAction">
<interceptor-ref name ="defaultStack" />
<interceptor-ref name ="fileUpload">
<param name ="allowedTypes">
image/bmp,image/png,image/gif,image/jpg
</param>
<param name="maximumSize">40480</param>
</interceptor-ref>
<result name = "input">
/upImages.jsp
</result>
<result name="success" type = "redirect">
http://www.baidu.com
</result>
<result name="error" type = "redirect">
http://www.baidu.com
</result>
<result name="show">
/showPicture.jsp
</result>
</action>
</package>
</struts>
显示图片页面:showPicture.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示图片</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<img src="${sout }" title="tt">
</body>
</html>
应下面网友要求,先将action主要代码贴出来