Struts2实现上传下载

struts.xml配置:

<!--         文件上传下载                         -->
 <package name="uploadFile" extends="struts-default">  
  
        <action name="myUpload" class="com.UploadAction">  
            <interceptor-ref name="fileUpload">             
                <param name="savePath">/file</param>  
                <param name="maximumSize">1024000</param>  
            </interceptor-ref>  
            <interceptor-ref name="defaultStack" />  
            <result name="input">upload.jsp</result>  
            <result name="success">download.jsp</result>  
        </action>  
        
        
        <action name="download" class="com.DownLoadAction">  
            <!-- 设置文件名参数,由页面上传入 -->  
            <param name="fileName"></param>  
            <result name="success" type="stream">  
                <!-- 下载文件类型定义 -->  
                <param name="contentType">  
                    application/octet-stream  
                </param>  
                <!-- 下载文件处理方法 -->  
                <param name="contentDisposition">  
                    attachment;filename="${downloadChineseFileName}"  
                </param>  
                <!-- 下载文件输出流定义 -->  
                <param name="inputName">downloadFile</param>  
                <param name="bufferSize">4096</param>  
            </result>  
        </action>  
    </package> 


上传action:

package com;  
  
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 com.opensymphony.xwork2.ActionSupport;  
  
public class UploadAction extends ActionSupport {  
  
    private static final long serialVersionUID = 1L;  
    private File[] uploadFile;//上传的文件名   
    private String[] contentType;  
  
    private String[] caption;  
    private String[] uploadFilename;  
  
    public String[] getCaption() {  
        return caption;  
    }  
  
    public void setCaption(String[] caption) {  
        this.caption = caption;  
    }  
  
    public String[] getContentType() {  
        return contentType;  
    }  
  
    public void setUploadFileContentType(String[] contentType) {  
        this.contentType = contentType;  
    }  
  
    public File[] getUploadFile() {  
        return uploadFile;  
    }  
  
    public void setUploadFile(File[] uploadFile) {  
        this.uploadFile = uploadFile;  
    }  
  
    public String[] getUploadFilename() {  
        return uploadFilename;  
    }  
  
    public void setUploadFileFileName(String[] uploadFilename) {  
        this.uploadFilename = uploadFilename;   
    }  
  
    /** 
     * 拷贝文件到相应的目录下 
     * @param src 
     * @param dst 
     */  
    private static void copy(File src, File dst) {  
        try {  
            InputStream in = null;  
            OutputStream out = null;  
            try {  
                in = new BufferedInputStream(new FileInputStream(src));  
                out = new BufferedOutputStream(new FileOutputStream(dst));  
                byte[] buffer = new byte[16 * 1024];  
                while (in.read(buffer) > 0) {  
                    out.write(buffer);  
                }  
            } finally {  
                if (null != in) {  
                    in.close();  
                }  
                if (null != out) {  
                    out.close();  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    @Override  
    public String execute() {  
        for (int i = 0; i < this.getUploadFile().length; i++) {  
            File imageFile = null;            
                imageFile = new File(ServletActionContext.getServletContext()  
                        .getRealPath("/")  
                        + "file/" + new String(uploadFilename[i]));  
                  
            copy(this.getUploadFile()[i], imageFile);  
        }  
          
        return SUCCESS;  
    }     
      
} 


下载action:

package com;  
  
import java.io.File;  
import java.io.InputStream;  
import java.io.UnsupportedEncodingException;  
  
import org.apache.struts2.ServletActionContext;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class DownLoadAction extends ActionSupport {  
    private static final long serialVersionUID = 1L;  
    private String savePath = "file//";//文件上传的路径  
    private String fileFileName;  
    private File file;  
    private String fileContentType;  
  
    public String getFileName() {  
        return fileFileName;  
    }  
  
    public void setFileName(String fileName) {  
        this.fileFileName = fileName;  
    }  
  
    public File getFile() {  
        return file;  
    }  
  
    public void setFile(File file) {  
        this.file = file;  
    }  
  
    public String getFileContentType() {  
        return fileContentType;  
    }  
  
    public void setFileContentType(String fileContentType) {  
        this.fileContentType = fileContentType;  
    }  
  
    public String getFileFileName() {  
        return fileFileName;  
    }  
  
    public void setFileFileName(String fileFileName) {  
        this.fileFileName = fileFileName;  
    }  
  
    public String getSavePath() {  
        return savePath;  
    }  
  
    public void setSavePath(String savePath) {  
        this.savePath = savePath;  
    }  
  
    @Override  
    public String execute() throws Exception {  
        fileFileName = ServletActionContext.getRequest().getParameter(  
                "fileName");  
        return SUCCESS;  
    }  
  
    // 从下载文件原始存放路径读取得到文件输出流   
    public InputStream getDownloadFile() throws UnsupportedEncodingException {  
        fileFileName = new String(fileFileName.getBytes("ISO8859-1"), "UTF-8");  
        System.out.println("fileFileName:Down:"+fileFileName);  
        return ServletActionContext.getServletContext().getResourceAsStream(  
                savePath + fileFileName);  
    }  
  
    // 如果下载文件名为中文,进行字符编码转换   
    public String getDownloadChineseFileName() {  
        String downloadChineseFileName = fileFileName;  
  
        try {  
            downloadChineseFileName = new String(downloadChineseFileName  
                    .getBytes(), "ISO8859-1");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        return downloadChineseFileName;  
    }        
  
  
} 

在webroot下建一个file文件夹存放上传文件

前台上传:

<form action="myUpload" name="upload" enctype="multipart/form-data" method="post">
   		描述:<input type="text" name="caption"/><br>
   		<input type="file" name="uploadFile" /><br>
   		<input type="submit" value="提交" />
   		<input type="reset" value="重置"  />
   </form>


前台下载页面:

<%@ page language="java" contentType="text/html;charset=UTF-8"%>  
<%@ taglib prefix="s" uri="/struts-tags"%>  
  <%@ page import="java.io.*" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >  
<html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
        <title>查看上传文件</title>  
    </head>  
    <body>  
    <%  String path = request.getRealPath("/file");
    	File file = new File(path);
    	File[] files = file.listFiles();
    	for(int i=0;i<files.length;i++){
    	String filename = files[i].getName();
    	String filepath = files[i].getPath();
     %>
     <a href=download?fileName=<%=filename %>><%=filename %></a><br/>
     <%} %>
        <!-- <center>  
            <table border="1">  
                <s:iterator value="uploadFilename" status="stat">  
                    <tr>  
                        <td>  
                            <s:property value="%{uploadFilename[#stat.index]}" />  
                            <br />  
                        </td>  
                        </tr>  
                    <tr>  
                        <td>  
                            <s:property value="%{caption[#stat.index]}" />  
                        </td>  
                    </tr>  
                </s:iterator>  
            </table>  
        </center>   -->
    </body>  
</html> 


运行项目,即可实现功能


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值