File文件下载

五迷三道 提交于 2020-08-11 13:13:36
videoAppName: "xxx.exe"//配置文件中配置信息
@Value("${videoAppName}")
private String videoAppName;
/**
 * 返回exe文件
 * @return
 */
@ApiOperation("返回exe文件")
@GetMapping(value = "/getFile")
public void getFile(HttpServletResponse response){

    InputStream is = null;
    OutputStream os = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
        //文件地址
        String fileDownload = videoBasePath.replace("/",File.separator) + File.separator  + videoAppName;
        response.setContentType("applicaiton/x-download;charset=utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + videoAppName);

        is = new FileInputStream(new File(fileDownload));
        bis = new BufferedInputStream(is);
        os = response.getOutputStream();
        bos = new BufferedOutputStream(os);

        byte[] b = new byte[1024];
        int len = 0;
        while((len = bis.read(b)) != -1){
            bos.write(b,0,len);
        }
        bos.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if(bis != null)bis.close();
            if(is != null)is.close();
            if(bos != null)bos.close();
            if(os != null)os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!