• 下载工具类


    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();
                }
            }
        }
    }
    

    }

  • 相关阅读:
    Linux用户配置文件、口令配置文件、组配置文件
    Linux忘记Root密码怎么找回
    Linux运行级别及解释
    Maven获取resources的文件路径、读取resources的文件
    常见状态码100、200、300、400、500等
    JVM内存模型
    tcl使用小结
    MFQ&&PPDCS
    总结下自己在工作中有关联的TCP/IP协议
    二层交换机和三层交换机
  • 原文地址:https://www.cnblogs.com/fangyan1994/p/14030142.html
Copyright © 2020-2023  润新知