java下载文件到本地

代码实现

 /**
     * 下载通用配置指导书
     * @param request
     * @param response
     * @param filePath  文件路径
     */
    @Override
    public void downloadInstruction(HttpServletRequest request, HttpServletResponse response, String filePath) {
        File file = new File(filePath);
        String fileName = file.getName();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            request.setCharacterEncoding("UTF-8");
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("content-disposition", "attachement;fileName=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fis.read(bytes)) != -1) {
                response.getOutputStream().write(bytes, 0, len);
            }
            response.flushBuffer();
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

你可能感兴趣的:(java,开发语言,java-ee)