文章来源:http://blog.youkuaiyun.com/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主要代码贴出来
- public class MyUpAction extends ActionSupport{
- private File upload = null;
- private String uploadContentType;//文件类型,这两个属性,Struts2自动会解析
- private String uploadFileName; //文件名
- HttpServletResponse response = ServletActionContext.getResponse();
- ServletOutputStream sout;//二进制流可以直接在jsp页面显示
- public String execute(){
- System.out.println(upload);
- System.out.println(uploadContentType);
- System.out.println(uploadFileName);
- if(uploadFileName == null)
- {
- this.addFieldError("upload", "请选择文件");
- System.out.println("sdfasdf");
- }
- else{
- try{
- Images img = new Images();
- FileInputStream str=new FileInputStream(upload);
- Blob photo = Hibernate.createBlob(str);
- //img.setId(1);
- img.setImage(photo);
- ImagesDAO imagesDAO = new ImagesDAOImpl();
- imagesDAO.save(img);
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- return SUCCESS;
- }
- public String getImage(){
- ImagesDAO imagesDAO = new ImagesDAOImpl();
- Images img = imagesDAO.findById(3);
- Blob photo = img.getImage();
- try {
- InputStream in = photo.getBinaryStream();
- sout = response.getOutputStream();
- byte b[] = new byte[1024];
- int len = in.read(b);
- while(len!=-1){
- sout.write(b);
- len = in.read(b);
- }
- sout.flush();
- sout.close();
- in.close();
- } catch (SQLException e) {
- e.printStackTrace();
- return "error";
- }catch (IOException e){
- e.printStackTrace();
- return "error";
- }
- return "show";
- }
- }