struts2学习之多文件上传和下载(十三)

struts2文件上传和下载

1.多文件上传
1.编写filesuploads.jsp和success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="uploads" method="post" enctype="multipart/form-data">
    文件1:<input type="file" name="newbeedaly"/><br/>
    文件2:<input type="file" name="newbeedaly"/><br/>
    文件3:<input type="file" name="newbeedaly"/><br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
文件上传成功!
</body>
</html>

2.配置struts.xml文件

<action name="uploads" class="com.newbeedaly.action.FilesUploadAction">
            <result name="success">/success.jsp</result>
            <result name="input">/filesupload.jsp</result>
        </action>

3.编写FilesUploadAction类

package com.newbeedaly.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FilesUploadAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private File[] newbeedaly; // 文件

    private String[] newbeedalyFileName;  // 文件名

    private String[] newbeedalyContentType;  // 文件类型



    public File[] getnewbeedaly() {
        return newbeedaly;
    }



    public void setnewbeedaly(File[] newbeedaly) {
        this.newbeedaly = newbeedaly;
    }



    public String[] getnewbeedalyFileName() {
        return newbeedalyFileName;
    }



    public void setnewbeedalyFileName(String[] newbeedalyFileName) {
        this.newbeedalyFileName = newbeedalyFileName;
    }



    public String[] getnewbeedalyContentType() {
        return newbeedalyContentType;
    }



    public void setnewbeedalyContentType(String[] newbeedalyContentType) {
        this.newbeedalyContentType = newbeedalyContentType;
    }



    @Override
    public String execute() throws Exception {
        for(int i=0;i<newbeedaly.length;i++){
            System.out.println("文件名:"+newbeedalyFileName[i]);
            System.out.println("文件类型:"+newbeedalyContentType[i]);
            String filePath="C:/"+newbeedalyFileName[i];
            File saveFile=new File(filePath);
            FileUtils.copyFile(newbeedaly[i], saveFile);            
        }
        return SUCCESS;
    }




}

2.文件下载

  1. 编写fileDownload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="download">文件下载</a>
</body>
</html>

2.配置struts.xml文件

<action name="download" class="com.newbeedaly.action.FileDownloadAction">
            <result type="stream">
        <param name="contentDisposition">attachment;filename=${fileName}</param>
    </result>
</action>

3.编写类

package com.newbeedaly.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

public class FileDownloadAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String fileName;

    public String getFileName() throws Exception{
        fileName=new String(fileName.getBytes(),"ISO8859-1");
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public InputStream getInputStream()throws Exception{
        File file=new File("C:/美女1.jpg");
        this.fileName="美女1号";
        return new FileInputStream(file);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值