文件下载

文件下载

public void downloadFile(String fileCd, HttpServletResponse response) {
    WrBuMd wrBuMd = wrBuMdRepository.findById(fileCd).orElse(null);
    File file = new File(engingFileUrl + wrBuMd.getFilePath());
    FileInputStream fileOutputStream = null;
    OutputStream os = null;
    try {
        fileOutputStream = new FileInputStream(file);
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + new String(wrBuMd.getFileNm().getBytes("gb2312"), "ISO8859-1") + wrBuMd.getFileExt());
        os = response.getOutputStream();
        byte[] buffer = new byte[1024 * 2];
        int count = 0;
        while ((count = fileOutputStream.read(buffer)) != -1) {
            os.write(buffer, 0, count);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
posted @ 2020-07-15 23:21  JaminYe  阅读(119)  评论(0编辑  收藏  举报