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


流程:首先在add.jsp中选择要上传的文件或图片,点击确定.Struts中的ActionForm验证,验证无误就到Action类中继续操作,Action类调用PhotoDao类的addPhoto方法保存图片

    1:建表

    Sql代码
    create table(
      id int primary key auto_increment,
      name varchar(20),
      photo blob
    )

    create <A title=table href="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989 " target=_blank>table</A>(
      id int primary key auto_increment,
      name varchar(20),
      photo blob
    )

    create table(
      id int primary key auto_increment,
      name varchar(20),
      photo blob
    )


    2,add.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"%>

    <html>
        <head>
            <title>JSP for AddForm form</title>
        </head>
        <body>
            <html:form action="/add" enctype="multipart/form-data">
                name : <html:text property="name"/><html:errors property="name"/><br/>
                photo : <html:file property="photo"/><html:errors property="photo"/><br/>
                <html:submit/><html:cancel/>
            </html:form>
        </body>
    </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"%>

    <html>
        <head>
            <title>JSP for AddForm form</title>
        </head>
        <body>
            <html:form action="/add" enctype="multipart/form-data">
                name : <html:text property="name"/><html:errors property="name"/><br/>
                photo : <html:file property="photo"/><html:errors property="photo"/><br/>
                <html:submit/><html:cancel/>
            </html:form>
        </body>
    </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"%>

    <html>
     <head>
      <title>JSP for AddForm form</title>
     </head>
     <body>
      <html:form action="/add" enctype="multipart/form-data">
       name : <html:text property="name"/><html:errors property="name"/><br/>
       photo : <html:file property="photo"/><html:errors property="photo"/><br/>
       <html:submit/><html:cancel/>
      </html:form>
     </body>
    </html> 3:photo(javabean)代码如下

    Java代码
    package com.hp.entity;

    import java.sql.Blob;


    public class Photo implements java.io.Serializable {

        private Integer id;
        private String name;
        private Blob photo;

        public Photo() {
        }

        public Photo(String name) {
            this.name = name;
        }

        public Photo(String name, Blob photo) {
            this.name = name;
            this.photo = photo;
        }

        public Integer getId() {
            return this.id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Blob getPhoto() {
            return photo;
        }

        public void setPhoto(Blob photo) {
            this.photo = photo;
        }


    }
    view plaincopy to clipboardprint?
    package com.hp.entity;

    import java.sql.Blob;


    public class Photo implements java.io.Serializable {

        private Integer id;
        private String name;
        private Blob photo;

        public Photo() {
        }

        public Photo(String name) {
            this.name = name;
        }

        public Photo(String name, Blob photo) {
            this.name = name;
            this.photo = photo;
        }

        public Integer getId() {
            return this.id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

         public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Blob getPhoto() {
            return photo;
        }

        public void setPhoto(Blob photo) {
            this.photo = photo;
        }


    }

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3