struts2檔案上傳

來源:互聯網
上載者:User

標籤:fileutils   名稱   name   evm   介面   ;;   jpeg   cti   return   

1.建立一個index.jsp介面 和success.jsp上傳成功介面

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>    <body>      <s:form action="files/add"  method="post" enctype="multipart/form-data">        <s:file label="圖片上傳" name="upload"/>         <s:submit value="提交"/>    </s:form>

success.jsp

  <body>    檔案操作成功!!  </body>

2.建立對應的struts.xml檔案

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!--設定檔案上傳的大小  --><constant name="struts.multipart.maxSize" value="5000000000"></constant><constant name="struts.devMode" value="true"/> <package name="default" namespace="/files" extends="struts-default">    <action name="add" class="cn.bdqn.action.UploadAction" method="upload">  <!-- 限制檔案只能是圖片  <interceptor-ref name="fileUpload">    <param name="allowedTypes">image/jpeg</param>  </interceptor-ref>  <interceptor-ref name="defaultStack"></interceptor-ref> -->    <result>/success.jsp</result>    <result  name="input">/index.jsp</result>  </action> </package></struts>

3.建立對應的UploadAction

屬性名稱的規範  底層代碼 FileUploadInterceptor中

 File[] files = multiWrapper.getFiles(inputName);                    if (files != null && files.length > 0) {                        List<File> acceptedFiles = new ArrayList<File>(files.length);                        List<String> acceptedContentTypes = new ArrayList<String>(files.length);                        List<String> acceptedFileNames = new ArrayList<String>(files.length);                        String contentTypeName = inputName + "ContentType";                        String fileNameName = inputName + "FileName";

 

public class UploadAction extends ActionSupport {   private  File upload;   //上傳的檔案  和前台file標籤中的name屬性 一致   private  String uploadFileName;   //上傳的檔案名稱   底層有規定,必須按照規範來   private  String uploadContentType;   //上傳的檔案的類型             //檔案上傳的方法    public  String  upload(){        System.out.println("進入了檔案的上傳......");        //指定檔案上傳的路徑        String  path=ServletActionContext.getServletContext().getRealPath("/upload");        //File.separatorChar  系統分隔字元  區別linux和windows系統        String pathName=path+File.separatorChar+uploadFileName;        System.out.println("上傳檔案的全名稱:"+pathName);        try {            FileUtils.copyFile(upload, new File(pathName));        } catch (IOException e) {            e.printStackTrace();            return INPUT;        }                return  SUCCESS;    }    public File getUpload() {        return upload;    }    public void setUpload(File upload) {        this.upload = upload;    }    public String getUploadFileName() {        return uploadFileName;    }    public void setUploadFileName(String uploadFileName) {        this.uploadFileName = uploadFileName;    }    public String getUploadContentType() {        return uploadContentType;    }    public void setUploadContentType(String uploadContentType) {        this.uploadContentType = uploadContentType;    }        }

 4.多檔案上傳 建立index2.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>    <body>      <s:form action="files/adds"  method="post" enctype="multipart/form-data">        <s:file label="圖片上傳" name="upload"/>        <s:file label="圖片上傳" name="upload"/>        <s:file label="圖片上傳" name="upload"/>         <s:submit value="提交"/>    </s:form>    </body>

5.在struts.xml檔案中 新增

 <!-- 多個檔案上傳 -->  <action name="adds" class="cn.bdqn.action.UploadAction2" method="upload">    <result>/success.jsp</result>    <result  name="input">/index2.jsp</result>  </action>

6.建立對應的UploadAction2

public class UploadAction2 extends ActionSupport {   private  File [] upload;   //上傳的檔案   private  String [] uploadFileName;   //上傳的檔案名稱   private  String [] uploadContentType;   //上傳的檔案的類型         //檔案上傳的方法    public  String  upload(){        System.out.println("進入了多檔案的上傳......");         String path= ServletActionContext.getServletContext().getRealPath("/upload");         String pathName="";          //遍曆 使用者選擇的檔案         for (int i = 0; i < upload.length; i++) {             pathName= path+File.separatorChar+uploadFileName[i];             try {                 //把使用者選擇的檔案複製到 指定的檔案下                FileUtils.copyFile(upload[i], new  File(pathName));            } catch (IOException e) {                e.printStackTrace();                return  INPUT;            }        }                 return  SUCCESS;    }    public File[] getUpload() {        return upload;    }    public void setUpload(File[] upload) {        this.upload = upload;    }    public String[] getUploadFileName() {        return uploadFileName;    }    public void setUploadFileName(String[] uploadFileName) {        this.uploadFileName = uploadFileName;    }    public String[] getUploadContentType() {        return uploadContentType;    }    public void setUploadContentType(String[] uploadContentType) {        this.uploadContentType = uploadContentType;    }}

 

struts2檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.