用StringBoot搭建一个简单的下载平台。

利用stringBoot搭建一个简单的下载平台。

 

@RestController
@RequestMapping("download")
public class DownLoadCollector {

    @GetMapping("/down")
    public void downLoad(@RequestParam("version") String version, HttpServletResponse res){
        System.out.println("version - > " + version);
        String filePath = "文件本地路径";
        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = res.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("success");
    }
}

 

 

 

你可能感兴趣的:(工作常用)