我要啦免费统计

          微藍領域

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

          Struts中上傳文件-formfile的應用

          Posted on 2007-08-26 18:26 hilor 閱讀(1580) 評論(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");
          }
          }





          主站蜘蛛池模板: 琼海市| 独山县| 富顺县| 水富县| 日土县| 新乡县| 永州市| 定安县| 大厂| 阿拉善右旗| 宁河县| 莲花县| 罗田县| 安义县| 北安市| 临汾市| 彝良县| 海口市| 德化县| 罗山县| 新乐市| 合水县| 东安县| 诸暨市| 新河县| 大理市| 阳江市| 尼勒克县| 呼图壁县| 来安县| 平武县| 巨鹿县| 宣城市| 富顺县| 大埔区| 榆社县| 南丰县| 石城县| 霍邱县| 二连浩特市| 大埔县|