使用Struts中的ActionForm類來上傳圖片

來源:互聯網
上載者:User
  1. 用actionform上傳檔案
  2. ===========================================================================================================================
  3. action寫法
  4. public class CompanyAddAction extends Action {
  5.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  6.             HttpServletRequest request, HttpServletResponse response) {
  7. //使用Struts中的UploadForm類來上傳圖片,也可以用其他上傳圖片的控制項,在網路上有很多
  8.   //取得機構LOGO並上傳
  9.             String encoding = request.getCharacterEncoding();
  10.             if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
  11.                 response.setContentType("text/html;charset=gb2312");
  12.             }//將頁面編碼轉換為 gb2312簡體中文
  13.             UploadForm uf = (UploadForm) form;//將擷取的form對象強制轉換為UploadForm類型
  14.             FormFile file1 = uf.getOrPicDir();//調用UploadForm中的getOrPicDir()方法擷取上傳的檔案對象賦給FormFile                                                            //類的對象
  15.             String size1 = (file1.getFileSize() + " bytes");// 調用FormFile類的對象file1的getFileSize()方發擷取文                                                                        //件大小
  16.             String fileName1 = file1.getFileName();// 調用FormFile類的對象file1的getFileName()方發擷取文                                                                        //件名
  17.             //System.out.println("fileName1=" + fileName1);
  18.             try {
  19.                 InputStream stream = file1.getInputStream();//建立一個輸入資料流把file1對象讀入
  20.                 String filePath = servlet.getServletContext().getRealPath("/pictureorg");//把伺服器                                                                                               //中"/pictureorg"檔案路徑儲存為字 符串
  21.                 OutputStream bos = new FileOutputStream(filePath + "/" + fileName1);
  22.                 // 建立一個上傳檔案的輸出資料流,將上傳檔案存入web應用的根目錄。
  23.                 int bytesRead = 0;
  24.                 byte[] buffer = new byte[8192];//設定緩衝區大小
  25.                 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
  26.                     bos.write(buffer, 0, bytesRead);// 調用write()方法把檔案寫入伺服器
  27.                 }
  28.                 
  29.                 uf.setOrPicDir(filePath + "/" + file1.getFileName());//將檔案資訊與檔案名稱set入uf對象
  30.                 //System.out.println(uf.getOrPicDir());
  31.                 bos.close();
  32.                 stream.close();
  33.             } catch (Exception e) {
  34.                 System.err.print(e);
  35.                 uf.setOrPicDir("");//異常時檔案資訊設定為空白
  36.             }
  37.             
  38. ===================================================================================================================
  39. jap中寫法
  40. form可以使用普通form,<form name="form1" method="post" action="OrgAdd.do" enctype="multipart/form-data">
  41.  <input name="orPicDir" size="35" type="file">
  42. ==================================================================================================================
  43. actionform寫法
  44. import javax.servlet.http.HttpServletRequest;
  45. import org.apache.struts.action.ActionErrors;
  46. import org.apache.struts.action.ActionForm;
  47. import org.apache.struts.action.ActionMapping;
  48. import org.apache.struts.upload.FormFile;
  49. /** 
  50.  * MyEclipse Struts
  51.  * Creation date: 06-13-2008
  52.  * 
  53.  * XDoclet definition:
  54.  * @struts.form name="uploadForm"
  55.  */
  56. public class UploadForm extends ActionForm {
  57.     /*
  58.      * Generated fields
  59.      */
  60.     /** picDir property */
  61.     private FormFile picDir;
  62.     /** orPicDir property */
  63.     private FormFile orPicDir;
  64.     /*
  65.      * Generated Methods
  66.      */
  67.     /** 
  68.      * Method validate
  69.      * @param mapping
  70.      * @param request
  71.      * @return ActionErrors
  72.      */
  73.     public ActionErrors validate(ActionMapping mapping,
  74.             HttpServletRequest request) {
  75.         // TODO Auto-generated method stub
  76.         return null;
  77.     }
  78.     /** 
  79.      * Method reset
  80.      * @param mapping
  81.      * @param request
  82.      */
  83.     public void reset(ActionMapping mapping, HttpServletRequest request) {
  84.         // TODO Auto-generated method stub
  85.     }
  86.     /** 
  87.      * Returns the picDir.
  88.      * @return String
  89.      */
  90.     public FormFile getPicDir() {
  91.         return picDir;
  92.     }
  93.     /** 
  94.      * Set the picDir.
  95.      * @param picDir The picDir to set
  96.      */
  97.     public void setPicDir(FormFile picDir) {
  98.         this.picDir = picDir;
  99.     }
  100.     /** 
  101.      * Returns the orPicDir.
  102.      * @return String
  103.      */
  104.     public FormFile getOrPicDir() {
  105.         return orPicDir;
  106.     }
  107.     /** 
  108.      * Set the orPicDir.
  109.      * @param orPicDir The orPicDir to set
  110.      */
  111.     public void setOrPicDir(FormFile orPicDir) {
  112.         this.orPicDir = orPicDir;
  113.     }
  114.     public void setOrPicDir(String string) {
  115.         // TODO Auto-generated method stub
  116.         
  117.     }
  118. }
  119. ======================================================================================================================

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.