都督

导航

用ssh写的一个存储图片的程序,报了一个错,看大家有什么好的解决方法

       一般网站在处理用户上传图片时通常采用两种策略:一是直接把图片存入数据库中的Blob字段;二是数据库中只存储图片的在服务器上的路径信息 ,图片存放在分门别类的文件中,使用的时候从数据库读取路径信息到页面img元素即可.在此不讨论两种方案的优劣,我只是写了个hibernate的例子 来实现第一种策略.例子很简单,photo表用photos存储图片,photo字段类型为Blob.在此例中数据库我采用 mysql。

这个是我的Photo.cfg.xml文件

<hibernate-mapping>
    <class name="com.duqiao.channel.domain.PhotoDomain" table="PHOTO"
        lazy="false">
        <id name="id" column="ID" type = "int">
            <generator class="native"/>
        </id>
        <property name="userId" column="userId " />
        <property name="channelId" column="channelId" />
        <property name="level"    column="level" />
        <property name="refreshTime" column="refreshTime" />
        <property name="title" column="title" />
        <property name="photos" column="photos" type="java.sql.Blob"/>
        <property name="rsg1" column="rsg1" />
        <property name="rsg2" column="rsg2" />
        <property name="rsg3" column="rsg3" />
        <property name="createtime" column="createtime" />
        
        <!--    <property name="acreatTime" column="acreatTime" />
        <set name="topics">
            <key column="articleId"/>
            <one-to-many class="com.duqiao.channel.domain.TopicDomain"/>
        </set>-->
    </class>
</hibernate-mapping>

这个是我存储图片的代码

PhotoDomain photo = new PhotoDomain();
        UserDomain domain = (UserDomain) session.get("user");
        FileInputStream in = new FileInputStream(getPhotos().toString());
        Blob insertphoto = Hibernate.createBlob(in);
        photo.setPhotos(insertphoto);
        photo.setTitle(getTitle());
        photo.setRefreshTime(TimeUtil2.getCurrDate());
        photo.setUserId(domain.getId());
        photo.setLevel(0);
        photo.setRsg1("");
        photo.setRsg2("");
        photo.setRsg3(0);
        photo.setCreatetime(TimeUtil2.getCurrDate());
        this.photoService.add(photo);

这个是我查询后返回到页面的代码:

    phototes = this.photoService.findAllPhoto();
        int size = phototes.size();
        Blob photoMain = phototes.get(size - 1).getPhotos();
        InputStream into = null;
        OutputStream out = null;
        try {
            into = photoMain.getBinaryStream();
            out = response.getOutputStream();

            byte[] buf = new byte[1024];
            int len;
            while ((len = into.read(buf)) != -1) {

                out.write(buf, 0, len);
            }
            out.close();
            out.flush();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } catch (java.lang.IllegalStateException ie) {
            ie.printStackTrace();
        } catch (java.lang.NullPointerException le) {
            le.printStackTrace();
        } finally {
            in.close();
        }
        return "success";

图片能正常显示到页面但是,后台报个错java.lang.IllegalStateException: getOutputStream() has already been called for this response

我也上网查了说是这个是两个方法冲突了,在jsp页面后面加上<%=out.clear());out=pageContext.pushBody();%>,并把<%=%>中的空格去了就ok了,但是我的这段输代码是在java中写的,谢了这个也并不好用,看看哪位大牛能帮上忙。

 

 

posted on 2012-11-01 16:02  都督  阅读(716)  评论(7编辑  收藏  举报