熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

用 struts 向數據庫中儲存圖片

2022-06-13   來源: Java開源技術 

  這個例子是通過用Struts的FormFile來寫入到MySQL中
  
  用用戶通過選一個圖片然後按submit就可以存入數據庫中
  
  其中先要建立一個表:
  
  create table test ( name varchar() pic blob );在MySQL的test庫中
  
  <%@ page language=java%>
  <%@ taglib uri=bean prefix=bean%>
  <%@ taglib uri=html prefix=html%>
  
  <html>
  <head>
  <title>JSP for multiForm form</title>
  </head>
  <body>
  <html:form action=/multi enctype=multipart/formdata>一定要用enctype=multipart/formdata不然就提交之後就會有拋出異常
  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>
  
   相對應的ActionForm:
  
  //Created by MyEclipse Struts
  // XSL source (default): platform:/plugin/comgenuitececlipsecrosseasystrutseclipse_/xslt/JavaClassxsl
  
  package saoostrutsform;
  
  import orgapachestrutsactionActionForm;
  import orgapachestrutsuploadFormFile;
  
  /**
  * MyEclipse Struts
  * Creation date:
  *
  * 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) {
  thisfile = 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) {
  thisname = name;
  }
  }
  
   對就的Action:
  
  //Created by MyEclipse Struts
  // XSL source (default): platform:/plugin/comgenuitececlipsecrosseasystrutseclipse_/xslt/JavaClassxsl
  
  package saoostrutsaction;
  
  import javaioFileNotFoundException;
  import javaioIOException;
  import javasqlConnection;
  import javasqlDriverManager;
  import javasqlPreparedStatement;
  import javasqlSQLException;
  
  import javaxservlethttpHttpServletRequest;
  import javaxservlethttpHttpServletResponse;
  
  import orgapachestrutsactionAction;
  import orgapachestrutsactionActionForm;
  import orgapachestrutsactionActionForward;
  import orgapachestrutsactionActionMapping;
  import orgapachestrutsuploadFormFile;
  
  import saoostrutsformMultiForm;
  
  /**
  * MyEclipse Struts
  * Creation date:
  *
  * XDoclet definition:
  * @struts:action path=/multi name=multiForm input=/form/multijsp 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 = multiFormgetFile();
  
  String name = multiFormgetName();
  try {
  ClassforName(orggjtmmmysqlDriver);
  String url=jdbc:mysql:///test;
  Connection con=DriverManagergetConnection(urlrootpassword);
  String sql=insert into pic values (??);
  PreparedStatement ps =conprepareStatement(sql);
  pssetString( name);
  //加入圖片到數據庫
  pssetBinaryStream(filegetInputStream()filegetFileSize());
  psexecuteUpdate();
  psclose();
  conclose();
  } catch (SQLException se) {
  seprintStackTrace();
  return mappingfindForward(error);
  } catch (ClassNotFoundException e) {
  // TODO Autogenerated catch block
  eprintStackTrace();
  return mappingfindForward(error);
  } catch (FileNotFoundException e) {
  // TODO Autogenerated catch block
  eprintStackTrace();
  return mappingfindForward(error);
  } catch (IOException e) {
  // TODO Autogenerated catch block
  eprintStackTrace();
  return mappingfindForward(error);
  }
  return mappingfindForward(success);
  }
  }
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27905.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.