Struts2+Hibernate3将图片保存于mysql数据库并将其显示在JSP页面

文章来源: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主要代码贴出来

 

[java]  view plain copy
  1. public class MyUpAction extends ActionSupport{  
  2.     private File upload = null;  
  3.     private String uploadContentType;//文件类型,这两个属性,Struts2自动会解析  
  4.     private String uploadFileName;   //文件名  
  5.     HttpServletResponse response = ServletActionContext.getResponse();  
  6.     ServletOutputStream sout;//二进制流可以直接在jsp页面显示  
  7.   
  8.     public String execute(){  
  9.         System.out.println(upload);  
  10.         System.out.println(uploadContentType);  
  11.         System.out.println(uploadFileName);  
  12.         if(uploadFileName == null)  
  13.         {  
  14.             this.addFieldError("upload""请选择文件");  
  15.             System.out.println("sdfasdf");  
  16.         }  
  17.         else{  
  18.             try{  
  19.                 Images img = new Images();  
  20.                 FileInputStream str=new FileInputStream(upload);  
  21.                 Blob photo = Hibernate.createBlob(str);  
  22.                 //img.setId(1);  
  23.                 img.setImage(photo);  
  24.                 ImagesDAO imagesDAO = new ImagesDAOImpl();  
  25.                 imagesDAO.save(img);  
  26.             }catch(IOException e){  
  27.                 e.printStackTrace();  
  28.             }             
  29.         }         
  30.         return SUCCESS;  
  31.     }     
  32.     public String getImage(){         
  33.         ImagesDAO imagesDAO = new ImagesDAOImpl();  
  34.         Images img = imagesDAO.findById(3);  
  35.         Blob photo = img.getImage();  
  36.         try {  
  37.             InputStream in = photo.getBinaryStream();  
  38.             sout = response.getOutputStream();  
  39.             byte b[] = new byte[1024];  
  40.             int len = in.read(b);  
  41.             while(len!=-1){  
  42.                 sout.write(b);  
  43.                 len = in.read(b);  
  44.             }  
  45.             sout.flush();  
  46.             sout.close();  
  47.             in.close();  
  48.         } catch (SQLException e) {  
  49.             e.printStackTrace();  
  50.             return "error";  
  51.         }catch (IOException e){  
  52.             e.printStackTrace();  
  53.             return "error";  
  54.         }  
  55.         return "show";  
  56.     }  
  57. }  


你可能感兴趣的:(Struts2+Hibernate3将图片保存于mysql数据库并将其显示在JSP页面)