struts2上传下载

struts上传下载必须引入两个jar文件:

commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import pojo.User;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpAction extends ActionSupport{
    private static final int BUFFER_SIZE = 40*40;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    private User user;
    private static void copy(File source, File target){
        InputStream is = null;
        OutputStream os = null;
        try{
            is = new BufferedInputStream(new FileInputStream(source), BUFFER_SIZE);
            os = new BufferedOutputStream(new FileOutputStream(target), BUFFER_SIZE);
            int len = 0;
            byte[] bs = new byte[BUFFER_SIZE];
            while ((len=is.read(bs))!=-1) {
                os.write(bs, 0, len);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(is!=null){
                try{
                    is.close();
                }catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }
            if(os!=null){
                try{
                    os.close();
                }catch (Exception e3) {
                    // TODO: handle exception
                    e3.printStackTrace();
                }
            }
        }
    }
    
    @Override
    public String execute() throws Exception{
        String path = ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+this.getUploadFileName();
        user.setPhone(this.uploadFileName);
        File target = new File(path);
        copy(this.upload, target);
        return SUCCESS;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return savePath;
    }

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

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

struts2中的action配置

<action name="fileUp" class="actions.FileUpAction">
            <param name="savePath">/upload</param>
            <result>/userInfo.jsp</result>
</action>

表单

<s:form action="fileUp" namespace="/" method="post" enctype="multipart/form-data">
        <s:textfield name="user.name" label="姓名" size="20"/>
        <s:file name="upload" label="形象" size="20"/>
        <s:textfield name="user.age" label="年龄" size="20"/>
        <s:radio list="#{1:'男',2:'女' }" name="user.sex" listKey="key" listValue="value" value="1" label="性别" cssStyle="border:0px;"/>
        <s:textfield name="user.icard" label="身份证号" size="20"/>
        <s:textfield name="user.phone" label="联系电话" size="20"/>
        <s:textfield name="user.address" label="家庭住址" size="20"/>
        <s:submit value="确定录入" align="center"/>
</s:form>

如果表单中包含一个name属性为xxx的文件域,那么在Action中可以使用如下3个属性来封装文件域信息:

File xxx 封装文件域对应的文件内容

String xxxContextType 封装文件域对应文件的文件类型

String xxxFileName 封装文件域对应文件的文件名

使用图片的时候

<img src="upload/<s:property value="uploadFileName"/>"/>

 



 

下载

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
    private String downPath;
    public InputStream getInputStream() throws Exception{
        return ServletActionContext.getServletContext().getResourceAsStream(downPath);
    }
    public String getDownPath() {
        return downPath;
    }
    public void setDownPath(String downPath) {
        this.downPath = downPath;
    }
    public String getDownloadFileName(){
        String downFileName = downPath.substring(7);
        try{
            downFileName = new String(downFileName.getBytes(), "ISO8859-1");
        }catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return downFileName;
    }
    
    @Override
    public String execute() throws Exception{
        return SUCCESS;
    }

struts2.xml配置

<action name="downLoad" class="actions.DownloadAction">
            <result type="stream">
                <param name="contentType">
                    application/msword,text/plain,application/vnd.ms-powerpoint,application/vnd.ms-excel
                </param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">        
                    attachment;filename="${downloadFileName}"
                </param>
                <param name="bufferSize">40960</param>
            </result>
        </action>

使用

 <a href="downLoad.action?downPath=upload/123.png">下载</a>

转载于:https://www.cnblogs.com/tuifeideyouran/p/4351940.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值