程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 用Struts向MYSQL數據庫中儲存圖片實例

用Struts向MYSQL數據庫中儲存圖片實例

編輯:關於MYSQL數據庫

這個例子是通過用Struts的FormFile來寫入到MySQL中。用戶通過選一個圖片,然後按submit就可以存入數據庫中,其中先要建立一個表:

 

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");
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved