博客 / 詳情

返回

springboot文件下載功能開發!

1L祭天

近期擠到一個需求 基於docker集羣部署的環境下載各個服務器節點上的日誌文件(對應的服務文件路徑已經做了映射)

首先來看一下架構
image.png
思路: 所有的服務都有主備之分,收集日誌的思路即是在每個機器節點上安裝一個monitor.jar的監控服務,通過當前主程序去調用monitor 然後返回對應的數據

主程序代碼如下


public class LogDownloadController {

    @GetMapping("loadLOgList")
    public Resp loadLOgList(@RequestParam("remoteIp") String remoteIp,@RequestParam("appName") String appName) {
        List<UploadFile> ja;
        try {
            String url = String.format("http://%s:%d/api/filelist/files/%s", remoteIp, 8887,appName);
            String res = HttpUtil.get(url, 1000);
            ja = JSONArray.parseObject(res, List.class);
        } catch (Exception e) {
            ja = Collections.emptyList();
            log.error(e.getMessage());
        }

        return Resp.OK(ja);
    }

    @GetMapping("loadLOgQue")
    public Resp loadLOgQue(@RequestParam("remoteIp") String remoteIp, @RequestParam("appName") String appName, @RequestParam("fileName") String fileName, HttpServletResponse httpServletResponse) {
        try {
            String url = String.format("http://%s:%d/api/filelist/files/%s/%s", remoteIp, 8887,fileName,appName);
            httpServletResponse.setContentType("application/x-download");
            httpServletResponse.setHeader("content-Disposition", "attachment;filename=" + fileName);
            ServletOutputStream outputStream = httpServletResponse.getOutputStream();
            HttpUtil.download(url, outputStream,true);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Resp.OK("success");
    }

}

monitor服務代碼如下:

@RestController
@RequestMapping("api/filelist")
public class FileListController {


    @Autowired
    FileStorageService fileStorageService;

    @GetMapping("/files/{appName}")
    public ResponseEntity<List<UploadFile>> files(@PathVariable("appName") String appName) {
        List<UploadFile> files = fileStorageService.loadListsByAppName(appName)
                .map(path -> {
                    String fileName = path.getFileName().toString();
                    String url = MvcUriComponentsBuilder
                            .fromMethodName(FileListController.class,
                                    "getFile",
                                    path.getFileName().toString(),appName
                            ).build().toString();
                    return new UploadFile(fileName, url);
                }).collect(Collectors.toList());
        return ResponseEntity.ok(files);
    }

    @GetMapping("/files/{filename:.+}/{appName}")
    public ResponseEntity<Resource> getFile(@PathVariable("filename") String filename,@PathVariable("appName") String appName) {
        Resource file = fileStorageService.load(filename,appName);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment;filename=\"" + file.getFilename() + "\"")
                .body(file);
    }

}
參考如下
springBoot2.0基於restFul風格的文件下載
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.