• Struts2文件下载


    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
        <form action="fileDownload.action" method="post">
            <input type="submit" value="Download"></input>
        </form>
    </body>
    </html>
    package com.oa.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * 
     * @author: zhao
     * @time: 2016年5月5日
     * 
     * @description:Struts2文件下载
     */
    @SuppressWarnings("all")
    public class FileDownloadAction extends ActionSupport{
        private static final long serialVersionUID = 1L;
        private InputStream inputStream;
        private String filename;
        
        @Override
        public String execute() throws Exception {
            File file=new File("E:\imooc.txt");
            filename=file.getName();
            try {
                inputStream=new FileInputStream(file);
                System.out.println(inputStream.available());
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return SUCCESS;
        }
        
        public InputStream getInputStream() {
            return inputStream;
        }
        public void setInputStream(InputStream inputStream) {
            this.inputStream = inputStream;
        }
        public String getFilename() {
            return filename;
        }
        public void setFilename(String filename) {
            this.filename = filename;
        }
        
    
    }
    <?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>
        <package name="default" namespace="/" extends="struts-default">
            <action name="fileDownload" class="com.oa.action.FileDownloadAction">
                <result type="stream">
                    <param name="inputName">inputStream</param>
                    <param name="contentDisposition">attachment;filename="${filename}"</param>
                    <param name="contentType">application/octet-stream</param>
                    <param name="bufferSize">1024</param>
                </result>
            </action>
    
        </package>
    
    </struts>
  • 相关阅读:
    用wamp配置的环境,想用CMD连接mysql怎么连
    Mysql删除表
    MySQL创建表
    Leetcode 130. Surrounded Regions
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 110. Balanced Binary Tree
    Leetcode 98. Validate Binary Search Tree
    Leetcode 99. Recover Binary Search Tree
    Leetcode 108. Convert Sorted Array to Binary Search Tree
    Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/zhao307/p/5463390.html
Copyright © 2020-2023  润新知