struts2上傳檔案

來源:互聯網
上載者:User

標籤:上傳檔案

1. 開發環境
Myeclipse     Version: 10.0    Build id: 10.0-20111028Struts2.3及commons-fileupload1.3.1、commons-io-2.2![上傳檔案所需jar包](http://img.blog.csdn.net/20150214155124054)簡單瞭解一下這些常用的jar包:    commons-logging:Tlog的介面。只是一個簡單的log介面,通常的使用會結合相關的實現工具來開發。例如:log4j、logKit等。當加入到項目中後,會根據項目中有哪些日誌的配置來自動去掃描並配置制定的日誌工具。通常項目配置為commons-logging和log4j。    freemarker:一種類似於EL、jstl運算式的介面顯示範本語言。    Commons-fileupload、commons-io:用於支援檔案上傳和檔案讀寫操作的第三方包。    ognl:在struts2特有的介面標籤顯示語言。一般情況很少用,因為jstl已經將這些定義更加規範和統一。    Struts2-core:struts2的核心包。    xwork:也是一款非常優秀的mvc架構,而struts2就是基於xwork來開發的。

一般的思路:對於此類問題,一般的思路就是從介面拿到對應檔案路徑,在代碼中讀取檔案輸出資料流,在代碼中寫入到指定路徑。很不幸的是struts2做了非常好的封裝,以至於更多的細節問題都屏蔽了。

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>        <!--開發人員模式 -->        <constant name="struts.devMode" value="true" />        <constant name="struts.i18n.encoding" value="utf-8" />        <package name="upload" namespace="/" extends="struts-default">            <action name="showUpload">                <result>/upload.jsp</result>            </action>            <action name="doUpload" class="com.cfl.StrutsFileUpload">                <result name="input">/upload.jsp</result>                <result>/upload_success.jsp</result>            </action>        </package>    </struts>

簡單介紹一下這個檔案的配置,檔案預設配置名為struts-default,是struts2的主要設定檔。主要的配置就是

     <action name="actionname">                <result>jsp路徑</result>     </action>
1.開發人員模式配置:
該配置中有 <constant name="struts.devMode" value="true" />,這個主要是將項目配置為開發人員模式。既然有開發人員模式,對應會有一個產品模式proMode。配置為開發人員模式之後,每次會重新載入配置的資源檔,不僅僅是jsp熱部署等。而是項目的設定檔如struts.xml、web.xml的每次變動都會重新載入。這樣對於開發人員來講,就不用每次在開發時都去重新啟動tomcat或者jboss了。但是隨著帶來也是效能的損耗。往往都是這樣效率的提高,伴隨著效能的下降。
2.extends=”struts-default”
在struts2-core的jar下面有這樣的一個檔案struts-default.xml,一個struts2的樣板配置。而這個配置中就有這樣一個struts-default,是用來配置struts2的18個核心filter的,算是struts2的一個約定吧。
Web.xml
<?xml version="1.0" encoding="UTF-8"?>    <web-app id="WebApp_ID" version="2.4"        xmlns="http://java.sun.com/xml/ns/j2ee"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">        <display-name>customization</display-name>        <!--清楚緩衝 -->        <filter>            <filter-name>struts-cleanup</filter-name>            <filter-class>                org.apache.struts2.dispatcher.ActionContextCleanUp            </filter-class>        </filter>          <filter>            <filter-name>struts2</filter-name>            <filter-class>                org.apache.struts2.dispatcher.FilterDispatcher            </filter-class>        </filter>        <filter-mapping>            <filter-name>struts-cleanup</filter-name>            <url-pattern>/*</url-pattern>        </filter-mapping>         <filter-mapping>            <filter-name>struts2</filter-name>            <url-pattern>/*</url-pattern>        </filter-mapping>    </web-app>

這個是最常見的設定檔了,作為整個項目的入口。該demo中配置了兩個分發器,分別是清理緩衝與分發控制的filter。在網上查閱資料中講,第一個分發器主要是用來清理緩衝的,如果沒有配置該過濾器可能在上傳檔案時無法擷取檔案。

Action
package com.cfl;import java.io.File;import javax.servlet.ServletContext;import org.apache.commons.io.FileUtils;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class StrutsFileUpload extends ActionSupport implements        ServletContextAware {    //檔案名稱    private File upload;    //檔案類型    private String uploadContentType;     //檔案名稱    private String uploadFileName;     private ServletContext context;    public String execute() throws Exception {        try {            //擷取伺服器檔案路徑            String serverFolder = context.getRealPath("/uploadFolder");            //擷取檔案名稱            String fileName = uploadFileName;            //建立檔案            File file = new File(serverFolder, fileName);            //將源檔案複製到目的檔案            FileUtils.copyFile(upload, file);                       setUploadFileName(file.getPath());        } catch (Exception e) {            addActionError(e.getMessage());            return INPUT;        }        return SUCCESS;    }    /**     * 得到上傳檔案     * @return     */    public File getUpload() {        return upload;    }    /**     * 設定上傳檔案     * @param upload     */    public void setUpload(File upload) {        this.upload = upload;    }    /**     * 得到上傳檔案類型     * @return     */    public String getUploadContentType() {        return uploadContentType;    }    /**     * 設定上傳檔案類型     * @param uploadContentType     */    public void setUploadContentType(String uploadContentType) {        this.uploadContentType = uploadContentType;    }    /**     * 得到上傳檔案名稱     * @return     */    public String getUploadFileName() {        return uploadFileName;    }    /**     * 設定上傳檔案類型     * @param uploadFileName     */    public void setUploadFileName(String uploadFileName) {        this.uploadFileName = uploadFileName;    }    /**     * 設定設定檔上下文     */    public void setServletContext(ServletContext context) {        this.context = context;    }}

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.