码迷,mamicode.com
首页 > Web开发 > 详细

Struts2:上传下载

时间:2017-01-05 01:22:40      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:stack   support   mic   actions   拦截器   ace   result   pac   单位   

ud_upload.jsp

    <s:form action="fileupload" enctype="multipart/form-data">
        <s:textfield label="照片描述" name="desc"></s:textfield>
        <s:file label="文件1" name="file1"></s:file>
        <s:submit value="上传"></s:submit>
    </s:form>

ud_download.jsp

    <s:url var="temp1" action="filedownload" ></s:url>
    <s:a href="%{temp1}" >下载bload.png</s:a>

struts.xml

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.custom.i18n.resources" value="messageResource" />
    <package name="p1" namespace="/" extends="struts-default">
        <action name="fileupload" class="org.ah.s2.C1" method="fileupload">
            <!-- 上传需要(自带)拦截器,传入参数 -->
            <interceptor-ref name="fileUpload">
                <!-- 定义允许上传的类型 -->
                <param name="allowedTypes">image/jpeg,image/png</param>
                <!-- 文件大小,单位:byte,不能用乘法计算,只能写最终数字 -->
                <!-- 35k -->
                <param name="maximumSize">35850</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack" />
            <result name="success" type="dispatcher">
                /ud_download.jsp
            </result>
            <!-- 文件被过滤掉,将返回input -->
            <result name="input">/ud_upload.jsp</result>
        </action>

<action name="filedownload" class="org.ah.s2.C1" method="filedownload"> <!-- 只有一个result子元素,不用name --> <result type="stream"> <param name="contentType">image/png</param> <!-- fileName对应下载后的文件名,这里就用Action中的变量了 --> <param name="contentDisposition">attachment;fileName=${downLoadAh}</param> <param name="inputName">inputStream</param> </result> </action> </package> </struts>
image/jpeg,不是jpg!
如果上传的文件不符合指定的要求,会回显错误信息。这些错误信息基于i18n,存放在struts-messages.properties配置文件中,所以需要配置struts.custom.i18n.resources

Action:

package org.ah.s2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class C1 extends ActionSupport {
    private String desc;// 描述
    private File file1;// 上传文件。java.io

    // 以下变量直接可得,文件名file1+固定后缀,都需要getter setter方法
    private String file1FileName;// 上传文件名
    private String file1ContentType;// 上传文件类型

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public File getFile1() {
        return file1;
    }

    public void setFile1(File file1) {
        this.file1 = file1;
    }

    public String getFile1FileName() {
        return file1FileName;
    }

    public void setFile1FileName(String file1FileName) {
        this.file1FileName = file1FileName;
    }

    public String getFile1ContentType() {
        return file1ContentType;
    }

    public void setFile1ContentType(String file1ContentType) {
        this.file1ContentType = file1ContentType;
    }

    /**
     * 文件上传
     * 
     * @return
     * @throws IOException
     */
    public String fileupload() throws IOException {

        // \t:制表符
        System.out.println("File name:" + this.file1FileName + "\t"
                + "ContentType:" + this.file1ContentType + "\t" + "描述:"
                + this.desc);

        // 文件拷贝
        FileInputStream fis = new FileInputStream(this.file1);

        FileOutputStream fos = new FileOutputStream("D:\\fileupload\\"
                + this.file1FileName);

        byte[] bs = new byte[1024];
        int real = fis.read(bs);
        while (real > 0) {
            fos.write(bs, 0, real);
            real = fis.read(bs);
        }

        fos.close();
        fis.close();

        return Action.SUCCESS;
    }

    // --------------------------------------------------

    // 下载时默认名称,只需getter方法即可
    private String downLoadAh;
    public String getDownLoadAh() {
        return downLoadAh;
    }

    /**
     * 文件下载
     * @return
     */
    public String filedownload() {
        downLoadAh = "border_1.png";
        return Action.SUCCESS;
    }

    // 用于下载的文件输入流
   // 对应:<param name="inputName">inputStream</param>
public InputStream getInputStream() { FileInputStream fis = null; try { fis = new FileInputStream(new File("D:\\fileupload\\" + downLoadAh)); } catch (FileNotFoundException e) { e.printStackTrace(); } return fis; } }

messageResource.properties

struts.messages.error.file.too.large=\u6587\u4EF6\u592A\u5927
struts.messages.error.file.extension.not.allowed=\u6587\u4EF6\u7C7B\u578B\u4E0D\u5339\u914D
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7C7B\u578B\u4E0D\u6B63\u786E

技术分享

 

Struts2:上传下载

标签:stack   support   mic   actions   拦截器   ace   result   pac   单位   

原文地址:http://www.cnblogs.com/AndyHoo/p/6250704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!