JAVA 下载单个文件

    public void toDownLoad(String ape505, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取文件后缀名
        String ext = ape505.substring(ape505.lastIndexOf(".") + 1).toUpperCase();
        // 读到流中
        // 文件的存放路径
        InputStream inStream = new FileInputStream( request.getSession().getServletContext().getRealPath("/upload/本级预警处理上传目录/")+ape505);
        // 设置输出的格式
        response.reset();
        response.setContentType(ext);
        response.addHeader("Content-Disposition", "attachment; filename=\"" + ape505 + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0) {
                response.getOutputStream().write(b, 0, len);
            }
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2020-08-11 11:29  林荼  阅读(258)  评论(0编辑  收藏  举报