struts檔案上傳

來源:互聯網
上載者:User

標籤:uri   jar   ast   報錯   package   大小   lib   ant   code   

一、編寫上傳介面
<body>  <form action="${pageContext.request.contextPath}/fileUploadAction" method="post" enctype="multipart/form-data">      檔案:<input type="file" name="file1"><br />      <input type="submit" value="上傳">  </form></body>
二、編寫後台Action
public class FileUpload extends ActionSupport{    //檔案:<input type="file" name="file1"><br />    private File file1;    private String file1FileName;    private    String file1ContentType;        public void setFile1(File file1) {        this.file1 = file1;    }    public void setFile1FileName(String file1FileName) {        this.file1FileName = file1FileName;    }    public void setFile1ContentType(String file1ContentType) {        this.file1ContentType = file1ContentType;    }        @Override    public String execute() throws Exception {        /*****處理業務******/        //吧檔案上傳到upload目錄        //擷取上傳的路徑        String path = ServletActionContext.getServletContext().getRealPath("/upload");        File destFile = new File(path,file1FileName);        FileUtils.copyFile(file1, destFile);        return SUCCESS;    }}
三、編寫設定檔upload.xml(注意:class值用點分割)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"          "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <package name="upload" extends="struts-default">    <!-- 注意class使用點 -->        <action name="fileUploadAction" class="cn.itcast.e_fileupload.FileUpload">            <result name="success">/e/success.jsp</result>            <!-- 配置錯誤視圖 -->            <result name="input">/e/error.jsp</result>        </action>    </package></struts>
四、把upload.xml引入struts.xml檔案
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"          "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <!-- 總的設定檔,引入其他檔案 -->    <include file="cn/itcast/e_fileupload/upload.xml"></include></struts>
五、配置(上傳檔案大小,以及類型)

1、struts預設上傳大小是2M;當檔案大於2M就會報錯,此時可以配置錯誤視圖error.jsp(可以顯示錯誤)。

 <body>  error.jsp<br />  <!-- 查看錯誤資訊 -->  <%@ taglib uri="/struts-tags" prefix="s" %>  <s:fielderror></s:fielderror> </body>

2、配置上傳檔案大小(struts-core-2.3.4.1.jar/org.apache.struts2/static/default.properties此檔案包含常量設定)

    在src目錄下,建立constant.xml

<struts>    <!-- 全域配置 -->        <!-- 上傳檔案大小配置 :30M-->    <constant name="struts.multipart.maxSize" value="31457280" /></struts>

3、配置上傳檔案類型(配置攔截器)

<struts>    <package name="upload" extends="struts-default">    <!-- 注意class使用點 -->        <action name="fileUploadAction" class="cn.itcast.e_fileupload.FileUpload">            <!-- 設定檔上傳的類型 -->            <interceptor-ref name="defaultStack">                <param name="fileUpload.allowedExtensions">txt,jpg</param>            </interceptor-ref>            <result name="success">/e/success.jsp</result>            <!-- 配置錯誤視圖 -->            <result name="input">/e/error.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.