• 文件下载工具类 DownLoadUtil 实战


    package com.cloud.mina.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;

    import javax.servlet.http.HttpServletResponse;
    /***
     * 下载文件工具类
     */

    public class DownLoadUtil {
        private static final String CONTENT_TYPE = "application/x-msdownload";
        /**
         * 下载文件
         * @param response HttpServletResponse
         * @param title 下载时候的文件名称
         * @param file 要下载的文件对象
         * @return true 下载成功    false 下载失败
         */
        public static boolean downLoadFile(HttpServletResponse response,String filename,File file){
            boolean result = true;
            FileInputStream input = null;
            OutputStream out = null;
            try {
                //设置response的编码方式
                response.setContentType(CONTENT_TYPE);
                //写明要下载的文件的大小
                response.setContentLength((int)file.length());
                //设置附加文件名
                response.setHeader("Content-Disposition","attachment;filename=""+new String
                        (filename.getBytes("UTF-8"),"iso-8859-1")+""");
                //读出文件到i/o流
                input =new FileInputStream(file);
                //从response对象中得到输出流,准备下载
                out = response.getOutputStream();
                if(input!=null && out!=null){    // 判断输入或输出是否准备好
                    int temp = 0 ;    
                    try{
                        while((temp=input.read())!=-1){    // 开始拷贝
                            out.write(temp) ;    // 边读边写
                        }
                    }catch(IOException e){
                        e.printStackTrace() ;
                        result = false;
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                result = false;
            } catch (IOException e) {
                result = false;
                e.printStackTrace();
            } finally{
                if(input!=null){
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        result = false; 
                    }
                }
                if(out!=null){
                    try {
                        out.flush();
                        out.close();//关闭输出流
                    } catch (IOException e) {
                        e.printStackTrace();
                        result = false; 
                    }
                }
                
            }
            return result;
        }
    }
     

    原文地址;https://blog.csdn.net/qxqxqzzz/article/details/97008529
  • 相关阅读:
    apache重写规则自动追加查询参数QSA
    错误代码2104:无法下载Silverlight应用程序。请查看Web服务器设置
    eclipse的shell相关插件
    二叉树及排序二叉树的相关操作汇总
    约瑟夫环
    c++ 输入一行字符串
    类对象做函数参数(传值和传引用)
    运算符重载(=和+)
    char型字符串(数组)与string型字符串 指针与引用
    一维和二维数组 动态内存分配
  • 原文地址:https://www.cnblogs.com/jpfss/p/11264546.html
Copyright © 2020-2023  润新知