posts - 325,  comments - 25,  trackbacks - 0

          一.單文件上傳
          添加jar包
          commons-fileupload-1.2.1.jar
          commons-io-1.4.jar
          在webroot創建一個文件如upload用于保存上傳的文件
          1.upload.jsp
           <body>
             <s:form action="upload" enctype="multipart/form-data">
             <s:file name="file" label="file"></s:file>
             <s:submit label="upload"></s:submit>
            </s:form>
           </body>

          2.UploadAction.java

          package com.test.upload;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.InputStream;
          import java.io.OutputStream;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.ActionSupport;

          public class UploadAction extends ActionSupport {
           private File file;
           private String fileFileName;
           private String fileContentType;
           public File getFile() {
            return file;
           }
           public void setFile(File file) {
            this.file = file;
           }
           public String getFileContentType() {
            return fileContentType;
           }
           public void setFileContentType(String fileContentType) {
            this.fileContentType = fileContentType;
           }
           public String getFileFileName() {
            return fileFileName;
           }
           public void setFileFileName(String fileFileName) {
            this.fileFileName = fileFileName;
           }
           @Override
           public String execute() throws Exception {
            //1.構建一個輸入流
            InputStream is=new FileInputStream(file);
            //2.構建一個上傳文件路徑
            String root=ServletActionContext.getRequest().getRealPath("/upload");
            //3.獲得一個本地文件
            File diskFile=new File(root,this.getFileFileName());
            //4.構建輸出流
            OutputStream os=new FileOutputStream(diskFile);
            //5.能過字節寫入輸出流
            byte[] buffer=new byte[400];
            int length=0;
            while((length=is.read(buffer))>0)
            {
             os.write(buffer,0,length);
            }
            is.close();
            os.close();
            return SUCCESS;
           }
          }

          3.配置struts.xml
          <constant name="struts.multipart.saveDir" value="c:\"></constant>
           <package name="FileuploadTest" extends="struts-default">
            <action name="upload" class="com.test.upload.UploadAction">
             <result name="success">/uploadSuccess.jsp</result>
            </action>
           </package>

          二.多文件上傳
          1.upload.jsp頁面
          <body>
            <s:form action="upload" enctype="multipart/form-data">
             <s:file name="file" label="file"></s:file>
             <s:file name="file" label="file1"></s:file>
             <s:file name="file" label="file2"></s:file>
             <s:submit label="submit"></s:submit>
            </s:form>
            </body>

          2.UploadAction.java

          package com.test.upload;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.List;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.ActionSupport;

          public class UploadAction extends ActionSupport {
           private List<File> file;

           private List<String> fileFileName;

           private List<String> fileContentType;

           public List<File> getFile() {
            return file;
           }

           public void setFile(List<File> file) {
            this.file = file;
           }

           public List<String> getFileContentType() {
            return fileContentType;
           }

           public void setFileContentType(List<String> fileContentType) {
            this.fileContentType = fileContentType;
           }

           public List<String> getFileFileName() {
            return fileFileName;
           }

           public void setFileFileName(List<String> fileFileName) {
            this.fileFileName = fileFileName;
           }

           @Override
           public String execute() throws Exception {
            for (int i = 0; i < file.size(); i++) {
             // 1.構建一個輸入流
             InputStream is = new FileInputStream(file.get(i));
             // 2.構建一個上傳文件路徑
             String root = ServletActionContext.getRequest().getRealPath(
               "/upload");
             // 3.獲得一個本地文件
             File diskFile = new File(root, this.getFileFileName().get(i));
             // 4.構建輸出流
             OutputStream os = new FileOutputStream(diskFile);
             // 5.能過字節寫入輸出流
             byte[] buffer = new byte[400];
             int length = 0;
             while ((length = is.read(buffer)) > 0) {
              os.write(buffer, 0, length);
             }
             is.close();
             os.close();
            }
            return SUCCESS;
           }
          }
          3.struts.xml
          同上

          posted on 2008-05-08 11:23 長春語林科技 閱讀(826) 評論(0)  編輯  收藏 所屬分類: struts2
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 富锦市| 扎鲁特旗| 铜山县| 大洼县| 玉树县| 永泰县| 沂南县| 门头沟区| 连城县| 重庆市| 加查县| 北票市| 彰武县| 阿巴嘎旗| 故城县| 娱乐| 洛川县| 合江县| 巴东县| 兴化市| 赞皇县| 灵山县| 浦江县| 定兴县| 仁布县| 额尔古纳市| 高安市| 曲松县| 深州市| 湖北省| 彰化县| 华安县| 连平县| 女性| 乌拉特后旗| 芮城县| 鹤岗市| 白城市| 兴化市| 武川县| 沙湾县|