首页下载分类最新更新汉化补丁下载排行国产软件国外软件注册破解资讯中心游戏娱乐
78软件站 - 首页
文章搜索: 分类 关键字
您的位置:首页网络编程 → 实例讲解如何用Struts向MySQL中储存图片
实例讲解如何用Struts向MySQL中储存图片
日期:2008-2-20 10:40:20 人气:0     [ ]
 
这个例子是通过用Struts的FormFile来写入到MySQL中。用户通过选一个图片,然后按submit就可以存入数据库中。 
1.首先需要建立一个表: 

create table test 
( name varchar(20), 
pic blob );
在MySQL的test库中
<%@ page language="java"%>
<%@ taglib uri=
"http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%> 
<%@ taglib uri=
"http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
 
<html> 
 <head>
  <title>JSP for multiForm form</title>
 </head>
 <body>
  <html:form action="/multi"
  enctype="multipart/form-data">
  一定要用enctype=“multipart/form-data“
  不然就提交之后就会有抛出异常
   file : <html:file property="file"/>
   <html:errors property="file"/></br>
   name : <html:text property="name"/>
   <html:errors property="name"/></br>
   <html:submit/><html:cancel/>
  </html:form>
 </body>
</html> 
2. 相对应的ActionForm: 

//Created by MyEclipse Struts
// XSL source (default): 
platform:/plugin/com.genuitec.eclipse.
cross.easystruts.eclipse_3.8.1
/xslt/JavaClass.xsl
package saoo.struts.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/** 
 * MyEclipse Struts
 * Creation date: 08-24-2004
 * 
 * XDoclet definition:
 * @struts:form name="multiForm"
 */
public class MultiForm extends ActionForm 
{
    // ----------------
 Instance Variables
    /** file property */
    private FormFile file;
    /** name property */
    private String name;
    // -----------------
Methods
    /** 
     * Returns the file.
     * @return FormFile
     */
    public FormFile getFile() 
 {
        return file;
    }
    /** 
     * Set the file.
     * @param file The file to set
     */
    public void setFile(FormFile file) 
 {
        this.file = file;
    }
    /** 
     * Returns the name.
     * @return String
     */
    public String getName()
 {
        return name;
    }
    /** 
     * Set the name.
     * @param name The name to set
     */
    public void setName(String name)
 {
        this.name = name;
    }


3. 对就的Action: 

//Created by MyEclipse Struts
// XSL source (default): 
platform:/plugin/com.genuitec.eclipse.
cross.easystruts.eclipse_3.8.1
/xslt/JavaClass.xsl
package saoo.struts.action;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
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 saoo.struts.form.MultiForm;
/** 
 * MyEclipse Struts
 * Creation date: 08-24-2004
 * 
 * XDoclet definition:
 * @struts:action path="/multi"
 name="multiForm" input="/form/multi.jsp" 
 scope="request"
 */
public class MultiAction extends Action
{
    // ---------------
 Instance Variables
    // ---------------
 Methods
    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) 
  {
        MultiForm multiForm = (MultiForm)
  form;
        FormFile file = multiForm.getFile();
        String name = multiForm.getName();
        try {
            Class.forName
   ("org.gjt.mm.mysql.Driver"); 
            String url="jdbc:mysql:
   ///test"; 
            Connection con=DriverManager.getConnection
   (url,"root","password"); 
            String sql="insert into pic values (?,?)"; 
            PreparedStatement ps
   =con.prepareStatement(sql); 
            ps.setString(1, name);
//加入图片到数据库
    
ps.setBinaryStream
(2,file.getInputStream(),
file.getFileSize());
            ps.executeUpdate(); 
            ps.close(); 
            con.close(); 
        } catch (SQLException se) 
  {
            se.printStackTrace();
            return mapping.findForward("error");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return mapping.findForward("error");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return mapping.findForward("error");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return mapping.findForward("error");
        }
        return mapping.findForward("success");
    }
出处:78软件收集整理 作者:不详
评论人 评论内容摘要(共 0 条,查看完整内容) 得分 0 发表时间
 热点文章
·Windows XP中快速转换FAT32至NTFS
·Windows 98全方位优化
·直接用WIN2000自带的NAT的实现端口映射的方法
·Ghost 2003成功克隆Windows XP
·全面提速Windows XP
·Windows XP优化技巧
·windows XP 系统服务“关闭”详细列表,释放N多内..
·Windows XP关机技巧二则
·Adobe Photoshop 鲜为人知的75个技巧[酷]
·给Windows XP减减肥
 推荐文章
·用好你的Windows安全模式
·想慢都不行:WinXP急速启动最新技巧
·各种ADSL路由设置
·解决 Windows XP_SP2 验证码无法显示的补丁!
·Windows Server 2003在线更新的补丁!
78软件站 版权所有 Copyright© 2003-2008 Www.78soft.COM, All Rights Reserved. Powered By 78软件站 鄂ICP备05000648号