Struts學習總結-04 上傳檔案

來源:互聯網
上載者:User

標籤:

1. upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags"  prefix="s"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>uploadDemo</title>  </head>    <body>        <s:form action="myUpload" enctype="multipart/form-data">            <s:file name="myfile"></s:file>        <s:submit value="submit"></s:submit>        </s:form>          </body></html>

2 uploadResult.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML  ><html>  <head>  </head>  <body>        Upload success!!      </body></html>

3 UploadAction.java

package com.xiping.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {    private File myfile; // 對應真正上傳的檔案    private String myfileFileName; // 對應上傳的檔案名稱    public File getMyfile() {        return myfile;    }    public void setMyfile(File myfile) {        this.myfile = myfile;    }    public String getMyfileFileName() {        return myfileFileName;    }    public void setMyfileFileName(String myfileFileName) {        this.myfileFileName = myfileFileName;    }    @Override    public String execute() throws Exception {        InputStream is = new FileInputStream(myfile);        OutputStream os = new FileOutputStream("E://temp3//" + myfileFileName);        byte[] buffer = new byte[1024];        int length = 0;        while (-1 != (length = is.read(buffer))) {            os.write(buffer, 0, length);        }        os.close();        is.close();        return SUCCESS;    }}

4 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>        <!-- 所有匹配*.action的請求都由struts2處理 -->    <constant name="struts.action.extension" value="action" />    <!-- 是否啟用開發模式 -->    <constant name="struts.devMode" value="true" />    <!-- struts設定檔改動後,是否重新載入 -->    <constant name="struts.configuration.xml.reload" value="true" />    <!-- 設定瀏覽器是否緩衝靜態內容 -->    <constant name="struts.serve.static.browserCache" value="false" />    <!-- 請求參數的編碼方式 -->    <constant name="struts.i18n.encoding" value="utf-8" />    <!-- 每次HTTP請求系統都重新載入資源檔,有助於開發 -->    <constant name="struts.i18n.reload" value="true" />    <!-- 檔案上傳最大值 -->    <constant name="struts.multipart.maxSize" value="104857600" />    <!-- 讓struts2支援動態方法引動過程 -->    <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <!-- Action名稱中是否還是用斜線 -->    <constant name="struts.enable.SlashesInActionNames" value="false" />    <!-- 允許標籤中使用運算式文法 -->    <constant name="struts.tag.altSyntax" value="true" />        <package name="struts" extends="struts-default">                            <action name="myUpload" class="com.xiping.action.UploadAction">                <result name="success">/uploadResult.jsp</result>            </action>                </package></struts>

 

Struts學習總結-04 上傳檔案

聯繫我們

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