• 下载工具类


    public class DownloadUtils {
    private static Logger logger = LoggerFactory.getLogger(com.hikvision.idatafusion.util.DownloadUtils.class);

    public DownloadUtils() {
    }
    
    /**
     * 从服务器下载文件
     * @param fileName
     * @param filepath
     * @param request
     * @param response
     * @throws Exception
     */
    public static void download(String fileName, String filepath, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("UTF-8");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        //String ctxPath = request.getSession().getServletContext().getRealPath("/") + filepath;
    
        try {
            File file = new File(filepath);
            if (!file.exists()) {
                logger.warn("download agent is error ! messages --->> " + filepath + " is not exists !");
            }
    
            response.setContentType("application/x-msdownload;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
            response.setHeader("Content-Length", String.valueOf(file.length()));
            bis = new BufferedInputStream(new FileInputStream(filepath));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
    
            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception var13) {
            logger.warn("download agent is error ! messages --->> " + var13.fillInStackTrace());
        } finally {
            if (bis != null) {
                bis.close();
            }
    
            if (bos != null) {
                bos.close();
            }
    
        }
    
    }
    
    /**
     * 下载二进制文件
     * @param fileName 文件名
     * @param bytes 文件的二进制字节数组
     * @param request
     * @param response
     * @throws Exception
     */
    public static void downloadFromBytes(String fileName,byte[] bytes, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        request.setCharacterEncoding("UTF-8");
        BufferedOutputStream bos = null;
        try {
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1"));
            bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(bytes);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    
    }
    
    
    /**
     * 从类路径下下载文件
     * @param response 返回
     * @param filePath 文件所在类路径
     * @param fileName 下载的文件名
     */
    public static void downLoadFromClasspath(HttpServletResponse response, String filePath, String fileName) {
        InputStream inputStream = null;
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource=resourceLoader.getResource(filePath);
        logger.info(resource.toString());
        BufferedInputStream bis=null;
        try {
            inputStream=resource.getInputStream();
            response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader(HttpHeaders.CONTENT_TYPE,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            BufferedOutputStream bos = new BufferedOutputStream(
                    response.getOutputStream());
            bis = new BufferedInputStream(inputStream);
            byte[] b=new byte[1024];
            int i = bis.read(b);
            while (i != -1) {
                bos.write(b, 0, b.length);
                bos.flush();
                i = bis.read(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
    
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    }

  • 相关阅读:
    管理者如何保持团队稳定性
    信息系统项目管理师考试之案例分析答题技巧
    【信息系统项目管理师】信息系统项目管理师计算题汇总
    [转]信息系统项目管理师计算题汇总
    Oracle
    PLSQL中查询数据的时候查询结果显示中文乱码
    项目管理知识体系指南(PMBOK 指南)
    linux查看分区命令和根分区扩容方法
    docker启动命令,docker重启命令,docker关闭命令
    iOS与JavaScript交互三: MessageHandler--window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
  • 原文地址:https://www.cnblogs.com/fangyan1994/p/14030142.html
Copyright © 2020-2023  润新知