Struts檔案上傳

來源:互聯網
上載者:User

標籤:value   utils   and   字串拼接   copy   filename   產生   path   tostring   

/* 檔案上傳的先決條件 * 1. 上傳的表單必須以POST方式提交(method="post") * 2. 提交需要指定編碼為: `multipart/form-data` (enctype="multipart/form-data`") * 3. 可以在xml設定檔中添加一個 name="input"的result, 便於錯誤資訊回顯 * 4. 如果下載的檔案太大, 需要在xml設定檔中修改預設的檔案的最大位元組數, 如最大允許上傳5M: *         ``` *         <constant name="struts.multipart.maxSize" value="5242880" /> *         ``` */

1.上傳的JSP頁面中的代碼:

  //引入S標籤

   <!-- from表單 -->    <s:fielderror fieldName="file"></s:fielderror> 錯誤資訊提示 <s:form action="/fileupload" method="post" theme="simple" enctype="multipart/form-data"> <!-- 選擇檔案標籤 --> <s:file name="file" /> <!-- submit按鈕 --> <s:submit value="上傳" /> </s:form>

2.具體Action類的操作:

public class FileUpLoadAction extends ActionSupport {    private static final long serialVersionUID = 1L;    //傳過來的檔案,file和jsp頁面的[<s:fielderror fieldName="file"></s:fielderror>]fieldName一樣        private File file;    //傳過來的檔案名稱:    private String fileFileName;    //傳過來的檔案類型:    private String  fileContentType;    
  產生三個欄位的getter,setter方法
//業務處理    public String execute() throws Exception {        //擷取到"/123"的路徑        String path = ServletActionContext.getServletContext().getRealPath("/123");        //將檔案類型進行"/"截取,得到最後一個元素[1],進行字串拼接        //fileName=UUID.檔案類型        String fileName=UUID.randomUUID().toString()+"."+fileContentType.split("/")[1];        //在"/123"中建一個名為fileName的檔案        File files=new File(path,fileName);        //將傳過來的檔案file拷貝到"/123"中名為fileName的檔案中        FileUtils.copyFile(file, files);        return NONE;    }

3.struts.xml中的代碼:

<struts>    <!-- 伺服器允許最大的上傳位元組大小 -->    <constant name="struts.multipart.maxSize" value="5242880"></constant>        <include file="struts-default.xml"></include>    <package name="fileuploads" extends="struts-default">        <action name="fileupload"  class="com.gxxy.filetransport.filedownload.FileUpLoadAction">                <interceptor-ref name="fileUpload">                <!-- 設定上傳檔案的位元組大小,這個大小隻能小於伺服器允許的最大的上傳位元組大小 -->                    <param name="maximumSize">1048576</param>                    <!-- 設定允許上傳的拓展名 -->                    <param name="allowedExtensions">png,jpg,txt</param>                </interceptor-ref>                <interceptor-ref name="defaultStack"/>            <result name="input">/JSP/filetransport/fileupload.jsp</result>            <result name="none">/JSP/filetransport/fileupload.jsp</result>        </action>    </package></struts>  

 

Struts檔案上傳

聯繫我們

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