使用Struts+Hibernate上传大对象(BLOB)(三)


    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;

    import org.apache.struts.action.ActionForm;

    import org.apache.struts.action.ActionForward;

    import org.apache.struts.action.ActionMapping;

    import org.apache.struts.upload.FormFile;

    import org.hibernate.Hibernate;

    import com.dao.*;

    import com.hp.entity.Photo;

    import com.hp.struts.form.AddForm;

    public class AddAction extends Action {

        public ActionForward execute(ActionMapping mapping, ActionForm form,

                HttpServletRequest request, HttpServletResponse response) {

            AddForm addForm = (AddForm) form;

            String name = addForm.getName();

            // 在Struts中上传文件需要使用FormFile

            FormFile formFile = addForm.getPhoto();

            system.out.println("name =" + name);

            system.out.println("size =" + formFile.getFileSize());

            Photo photo= new Photo();

            //将FormFile类型的图片信息转换为字节数组类型

            try {

                byte[] buf= formFile.getFileData();

                //使用Hibernate的工具将字节数组转换成Blob对象

                Blob ph= Hibernate.createBlob(buf);

                photo.setName(name);

                photo.setPhoto(ph);

                PhotoDao photoDao = new PhotoDao();

                photoDao.addPhoto(photo);

                //以上为插入图片,下面为读取刚才插入的图片,按name查找图片

                Photo temp = photoDao.getByName(name);

                //将BLOB对象转化为输入流的形式

                InputStream in=temp.getPhoto().getBinaryStream();

                //先在C盘生成temp文件,用于保存图片

                OutputStream os = new FileOutputStream(new File("c://temp.jpg"));

                //把输入流中的数据读入buf数组中

                in.read(buf);

                //从buf数组中读出数据

                os.write(buf);

                in.close();

                os.close();

                request.getSession().setAttribute("photo","c://temp.jpg");

                photoDao.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

            return mapping.findForward("success");

        }

        public static void main(String[] args) {

            system.out.println(System.getProperty("user.dir"));

        }

    }


    8:用于显示插入图片的页面show.jsp

    Html代码
    <%@ page language="java" pageEncoding="ISO-8859-1"%>

    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html:html lang="true">

      <head>

        <html:base />

        <title>show.jsp</title>

      </head>

      <body>

        <html:img src="${photo}"/>

      </body>

    </html:html>
    view plaincopy to clipboardprint?
    <%@ page language="java" pageEncoding="ISO-8859-1"%>

    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html:html lang="true">

      <head>

        <html:base />

        <title>show.jsp</title>

      </head>

      <body>

        <html:img src="${photo}"/>

      </body>

    </html:html>

本文作者:



相关阅读:
获取星期几的名称
ASP.Net调试之三板斧:第三招
SQL Server连接中三个常见的错误分析
CSS 清除浮动Clear
Oracle与Tomcat端口冲突
jQuery 点击图片跳转上一张或下一张功能的实现代码
日历控件在FF2.0,ie6下测试可用
PHP简介
mod_gzip:Apache的HTTP压缩优化
aspnet_isapi.dll' 报告它自身有问题的原因
如何更好的利用Oracle全文检索
PHP里如何实现汉字转区位码
Windows Server 2008 十项重要新技术
21个神奇的CSS技巧
快速导航

Copyright © 2016 phpStudy |