posts - 325,  comments - 25,  trackbacks - 0

          一.單文件上傳
          添加jar包
          commons-fileupload-1.2.1.jar
          commons-io-1.4.jar
          在webroot創(chuàng)建一個(gè)文件如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.構(gòu)建一個(gè)輸入流
            InputStream is=new FileInputStream(file);
            //2.構(gòu)建一個(gè)上傳文件路徑
            String root=ServletActionContext.getRequest().getRealPath("/upload");
            //3.獲得一個(gè)本地文件
            File diskFile=new File(root,this.getFileFileName());
            //4.構(gòu)建輸出流
            OutputStream os=new FileOutputStream(diskFile);
            //5.能過字節(jié)寫入輸出流
            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頁(yè)面
          <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.構(gòu)建一個(gè)輸入流
             InputStream is = new FileInputStream(file.get(i));
             // 2.構(gòu)建一個(gè)上傳文件路徑
             String root = ServletActionContext.getRequest().getRealPath(
               "/upload");
             // 3.獲得一個(gè)本地文件
             File diskFile = new File(root, this.getFileFileName().get(i));
             // 4.構(gòu)建輸出流
             OutputStream os = new FileOutputStream(diskFile);
             // 5.能過字節(jié)寫入輸出流
             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 長(zhǎng)春語林科技 閱讀(823) 評(píng)論(0)  編輯  收藏 所屬分類: struts2
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長(zhǎng)春語林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 丹江口市| 旅游| 祁东县| 衡山县| 普兰县| 迁安市| 会同县| 怀化市| 温州市| 吉安市| 普宁市| 林甸县| 启东市| 瓦房店市| 皮山县| 南川市| 大连市| 商南县| 韶山市| 曲阳县| 济南市| 桐梓县| 宿松县| 通城县| 闽侯县| 桂平市| 平昌县| 景谷| 石城县| 绥德县| 新巴尔虎左旗| 阜城县| 嵊州市| 牡丹江市| 平度市| 环江| 盐池县| 新宾| 阳新县| 平舆县| 迁西县|