欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式

 更新時(shí)間:2014年01月03日 15:28:12   作者:  
這篇文章主要介紹了struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式,有需要的朋友可以參考一下

通過2種方式模擬單個(gè)文件上傳,效果如下所示

開發(fā)步驟如下:

1、新建一個(gè)web工程,導(dǎo)入struts2上傳文件所需jar,如下圖

目錄結(jié)構(gòu)

2、新建Action

第一種方式

復(fù)制代碼 代碼如下:

package com.ljq.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{

    private File image; //上傳的文件
    private String imageFileName; //文件名稱
    private String imageContentType; //文件類型

    public String execute() throws Exception {
        String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        //D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
        System.out.println("realpath: "+realpath);
        if (image != null) {
            File savefile = new File(new File(realpath), imageFileName);
            if (!savefile.getParentFile().exists())
                savefile.getParentFile().mkdirs();
            FileUtils.copyFile(image, savefile);
            ActionContext.getContext().put("message", "文件上傳成功");
        }
        return "success";
    }

    public File getImage() {
        return image;
    }

    public void setImage(File image) {
        this.image = image;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }

    public String getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(String imageContentType) {
        this.imageContentType = imageContentType;
    }

   
}

第二種方式

復(fù)制代碼 代碼如下:

package com.ljq.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport {

    // 封裝上傳文件域的屬性
    private File image;
    // 封裝上傳文件類型的屬性
    private String imageContentType;
    // 封裝上傳文件名的屬性
    private String imageFileName;
    // 接受依賴注入的屬性
    private String savePath;

    @Override
    public String execute() {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            // 建立文件輸出流
            System.out.println(getSavePath());
            fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName());
            // 建立文件上傳流
            fis = new FileInputStream(getImage());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            System.out.println("文件上傳失敗");
            e.printStackTrace();
        } finally {
            close(fos, fis);
        }
        return SUCCESS;
    }

    /**
     * 返回上傳文件的保存位置
     *
     * @return
     */
    public String getSavePath() throws Exception{
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public File getImage() {
        return image;
    }

    public void setImage(File image) {
        this.image = image;
    }

    public String getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(String imageContentType) {
        this.imageContentType = imageContentType;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }

    private void close(FileOutputStream fos, FileInputStream fis) {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                System.out.println("FileInputStream關(guān)閉失敗");
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                System.out.println("FileOutputStream關(guān)閉失敗");
                e.printStackTrace();
            }
        }
    }

}

struts.xml配置文件

復(fù)制代碼 代碼如下:

<?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>
    <!-- 該屬性指定需要Struts2處理的請(qǐng)求后綴,該屬性的默認(rèn)值是action,即所有匹配*.action的請(qǐng)求都由Struts2處理。
        如果用戶需要指定多個(gè)請(qǐng)求后綴,則多個(gè)后綴之間以英文逗號(hào)(,)隔開。 -->
    <constant name="struts.action.extension" value="do" />
    <!-- 設(shè)置瀏覽器是否緩存靜態(tài)內(nèi)容,默認(rèn)值為true(生產(chǎn)環(huán)境下使用),開發(fā)階段最好關(guān)閉 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 當(dāng)struts的配置文件修改后,系統(tǒng)是否自動(dòng)重新加載該文件,默認(rèn)值為false(生產(chǎn)環(huán)境下使用),開發(fā)階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 開發(fā)模式下使用,這樣可以打印出更詳細(xì)的錯(cuò)誤信息 -->
    <constant name="struts.devMode" value="true" />
    <!-- 默認(rèn)的視圖主題 -->
    <constant name="struts.ui.theme" value="simple" />
    <!--<constant name="struts.objectFactory" value="spring" />-->
    <!--解決亂碼    -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!-- 指定允許上傳的文件最大字節(jié)數(shù)。默認(rèn)值是2097152(2M) -->
    <constant name="struts.multipart.maxSize" value="10701096"/>
    <!-- 設(shè)置上傳文件的臨時(shí)文件夾,默認(rèn)使用javax.servlet.context.tempdir -->
    <constant name="struts.multipart.saveDir " value="d:/tmp" />

        
    <package name="upload" namespace="/upload" extends="struts-default">
        <action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
            <result name="success">/WEB-INF/page/message.jsp</result>
        </action>
    </package>

    <package name="upload2" extends="struts-default">
        <action name="upload2" class="com.ljq.action.UploadAction2" method="execute">
            <!-- 動(dòng)態(tài)設(shè)置savePath的屬性值 -->
            <param name="savePath">/images</param>
            <result name="success">/WEB-INF/page/message.jsp</result>
            <result name="input">/upload/upload.jsp</result>
            <interceptor-ref name="fileUpload">
                <!-- 文件過濾 -->
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                <!-- 文件大小, 以字節(jié)為單位 -->
                <param name="maximumSize">1025956</param>
            </interceptor-ref>
            <!-- 默認(rèn)攔截器必須放在fileUpload之后,否則無效 -->
            <interceptor-ref name="defaultStack" />
        </action>
    </package>
</struts>

上傳表單頁面

復(fù)制代碼 代碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>文件上傳</title>

        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
    </head>

    <body>
        <!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
        <!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
        <form action="${pageContext.request.contextPath}/upload2/upload2.do"
              enctype="multipart/form-data" method="post">
            文件:<input type="file" name="image">
                <input type="submit" value="上傳" />
        </form>
        <br/>
        <s:fielderror />
    </body>
</html>

顯示結(jié)果頁面

復(fù)制代碼 代碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>上傳成功</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
  </head>

  <body>
    上傳成功!
    <br/><br/>
    <!-- ${pageContext.request.contextPath} tomcat部署路徑,
          如:D:\apache-tomcat-6.0.18\webapps\struts2_upload\ -->
    <img src="${pageContext.request.contextPath}/<s:property value="'images/'+imageFileName"/>">
    <s:debug></s:debug>
  </body>
</html>

相關(guān)文章

  • 簡單易懂講解happens-before原則

    簡單易懂講解happens-before原則

    Java內(nèi)存模型中的happens-before是什么?為什么會(huì)有這東西的存在?一個(gè)新東西肯定是上手先,但是等我們空下來回過頭來,我們還是需要去理解這些知識(shí),只有這樣我才能深刻的記住,并且運(yùn)用熟練。下來和小編來一起學(xué)習(xí)下
    2019-05-05
  • springboot?vue測試列表遞歸查詢子節(jié)點(diǎn)下的接口功能實(shí)現(xiàn)

    springboot?vue測試列表遞歸查詢子節(jié)點(diǎn)下的接口功能實(shí)現(xiàn)

    這篇文章主要為大家介紹了springboot?vue測試列表遞歸查詢子節(jié)點(diǎn)下的接口功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • SpringBoot部署在tomcat容器中運(yùn)行的部署方法

    SpringBoot部署在tomcat容器中運(yùn)行的部署方法

    這篇文章主要介紹了SpringBoot部署在tomcat容器中運(yùn)行的部署方法,需要的朋友可以參考下
    2018-10-10
  • Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))

    Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))

    這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)的相關(guān)資料,其中包括單向鏈表、雙向鏈表及鏈表反轉(zhuǎn)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2021-06-06
  • Java中null相關(guān)注解的實(shí)現(xiàn)

    Java中null相關(guān)注解的實(shí)現(xiàn)

    本文主要介紹了Java中null相關(guān)注解的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • java獲取服務(wù)器基本信息的方法

    java獲取服務(wù)器基本信息的方法

    這篇文章主要介紹了java獲取服務(wù)器基本信息的方法,涉及java獲取系統(tǒng)CPU、內(nèi)存及操作系統(tǒng)等相關(guān)信息的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Java List 用法實(shí)例詳解

    Java List 用法實(shí)例詳解

    這篇文章主要介紹了Java List 用法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-09-09
  • 詳解java調(diào)用存儲(chǔ)過程并封裝成map

    詳解java調(diào)用存儲(chǔ)過程并封裝成map

    這篇文章主要介紹了詳解java調(diào)用存儲(chǔ)過程并封裝成map的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-09-09
  • Java實(shí)現(xiàn)判斷瀏覽器版本與類型簡單代碼示例

    Java實(shí)現(xiàn)判斷瀏覽器版本與類型簡單代碼示例

    這篇文章主要介紹了Java實(shí)現(xiàn)判斷瀏覽器版本與類型簡單代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • java中類與對(duì)象的使用詳情

    java中類與對(duì)象的使用詳情

    這篇文章主要介紹了java中類與對(duì)象的使用詳情,面向?qū)ο笫峭ㄟ^類和對(duì)象去描述和代表萬千事物對(duì)象的,首先我們需要知道如何去定義一個(gè)類,下面文章我們?cè)敿?xì)介紹兩者的使用方法,需要的小伙伴可以參考一下
    2022-05-05

最新評(píng)論