我要啦免费统计

          微藍領域

          我的學習檔案館
          posts - 19, comments - 57, trackbacks - 0, articles - 57
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          公告

             如果大家在閱讀的過程中有什么問題,可以留言或發EMAIL給我. 我一定會回復并盡力給予答復.這也是相互促進的過程.. 有JAVA或J2EE方面的問題,也可以交流一下

          隨筆分類(15)

          搜索

          •  

          積分與排名

          • 積分 - 161712
          • 排名 - 370

          最新評論

          Struts中上傳文件-formfile的應用

          Posted on 2007-08-26 18:26 hilor 閱讀(1579) 評論(0)  編輯  收藏 所屬分類: Struts
          今天花了點時間學習了一下struts的commons-fileupload.jar,在網上找了個例子,用MyEclipse重新開發了一個,大約用時兩個小時,算是ok了!下面是系統應用的部分代碼:
          UploadsActionAction.java

            //Created by MyEclipse Struts
          // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

          package struts.action;

          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 struts.form.UploadsActionForm;
          import org.apache.struts.upload.FormFile;
          import java.io.*;
          /**
          * MyEclipse Struts
          * Creation date: 06-11-2006
          *
          * XDoclet definition:
          * @struts.action path="/uploadsAction" name="uploadsActionForm" input="/form/uploadsAction.jsp" scope="request" validate="true"
          */

          public class UploadsActionAction extends Action {

          // --------------------------------------------------------- Instance Variables

          // --------------------------------------------------------- Methods

          /**
          * Method execute
          * @param mapping
          * @param form
          * @param request
          * @param response
          * @return ActionForward
          */

          //UploadsActionForm uploadsActionForm = (UploadsActionForm) form;
          public ActionForward execute(ActionMapping mapping,
                      ActionForm form,
                      HttpServletRequest request,
                      HttpServletResponse response)
          throws Exception {

          String encoding = request.getCharacterEncoding();
          if ((encoding != null) && (encoding.equalsIgnoreCase(
          "utf-8")))
          {
          response.setContentType(
          "text/html; charset=gb2312");//如果沒有指定編碼,編碼格式為gb2312
          }
          UploadsActionForm theForm = (UploadsActionForm ) form;
          FormFile file = theForm.getFiles();
          //取得上傳的文件
          try {
          InputStream stream = file.getInputStream();
          //把文件讀入
          String filePath = request.getRealPath(
          "/upload");//上傳到指定的upload包中
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          OutputStream bos = new FileOutputStream(filePath +
          "/" +
                                         file.getFileName());
          //建立一個上傳文件的輸出流
          //System.out.println(filePath+"/"+file.getFileName());
          int bytesRead = 0;
          byte[] buffer = new byte[8192];
          while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
          bos.write(buffer, 0, bytesRead);
          //將文件寫入服務器
          }
          bos.close();
          stream.close();
          }catch(Exception e){
          System.err.print(e);
          }
          //request.setAttribute("dat",file.getFileName());
          return mapping.findForward(
          "display");

          }

          }

          UploadsActionForm.java

          //Created by MyEclipse Struts
          // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

          package struts.form;

          import org.apache.struts.action.ActionForm;
          import org.apache.struts.upload.FormFile;

          /**
          * MyEclipse Struts
          * Creation date: 06-11-2006
          *
          * XDoclet definition:
          * @struts.form name="uploadsActionForm"
          */

          public class UploadsActionForm extends ActionForm {
            
          //public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";

          // --------------------------------------------------------- Instance Variables

          /** theFile property */
          protected FormFile files;

          // ---------------------------------------------------------

          /**
          * Returns files.
          * @return FormFile
          */

          public FormFile getFiles() {
          return files;
          }

          /**
          * Set the files.
          * @param files The files to set
          */

          public void setFiles(FormFile file) {
          this.files = file;
          }

          }

          上傳用的頁面:uploadFile.jsp

          <%@ page language=
          "java"%>
          <%@ taglib uri=
          "/WEB-INF/struts-html.tld" prefix="html"%>

          <html>
          <head>
          <title>JSP for uploadsActionForm form</title>
          </head>
          <body>
          <html:form action=
          "/uploadsAction" enctype="multipart/form-data"> <1>
          theFile : <html:file property=
          "files"/><br/>
          <html:submit/><html:cancel/>
          </html:form>
          </body>
          </html>
          剛開始<1>處落下了
          "enctype="multipart/form-data" 費了我不少時間才搞定
          --------------------------------------------------------------------------------------------------------------



          FormBean:
          package onlyfun.caterpillar;

          import javax.servlet.http.*;
          import org.apache.struts.action.*;
          import org.apache.struts.upload.*;

          public class UploadForm extends ActionForm {
          private FormFile file;

          public void setFile(FormFile file) {
          this.file = file;
          }

          public FormFile getFile() {
          return file;
          }

          public void reset(ActionMapping mapping, HttpServletRequest req) {
          file = null;
          }
          }

          JSP:
          <html:form action="/Upload" method="post" enctype="multipart/form-data">
          選擇檔案:<html:file property="file" />
          <html:submit>上傳</html:submit>
          </html:form>

          Action:
          package onlyfun.caterpillar;

          import java.io.*;
          import javax.servlet.http.*;
          import org.apache.struts.action.*;
          import org.apache.struts.upload.*;

          public class UploadAction extends Action {
          public ActionForward execute(ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response)
          throws Exception {
          UploadForm fileForm = (UploadForm) form;
          FormFile file = fileForm.getFile();
          FileOutputStream fileOutput = new FileOutputStream("/home/caterpillar/files/" + file.getFileName());

          fileOutput.write(file.getFileData());
          fileOutput.flush();
          fileOutput.close();

          return mapping.findForward("success");
          }
          }





          主站蜘蛛池模板: 鄢陵县| 长寿区| 新沂市| 同德县| 浦县| 正镶白旗| 青铜峡市| 福州市| 新沂市| 老河口市| 肇州县| 油尖旺区| 孝义市| 祁东县| 孟津县| 醴陵市| 石城县| 光山县| 古丈县| 延津县| 象山县| 行唐县| 乌苏市| 北海市| 江都市| 丁青县| 罗平县| 涡阳县| 佛教| 红原县| 灵石县| 石渠县| 三河市| 和硕县| 枞阳县| 胶州市| 巩留县| 吉木萨尔县| 玛沁县| 墨江| 三江|