From 59cd84efa27ec07ee7c6ccf91bdafc63dbf87d11 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 22 Jul 2020 19:07:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E6=97=A5=E5=BF=97=E7=9A=84?= =?UTF-8?q?oom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PerformanceReportController.java | 21 +++++++++++-------- .../performance/service/ReportService.java | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java b/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java index b3d806ef85..178b6304f6 100644 --- a/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java +++ b/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java @@ -15,12 +15,11 @@ import io.metersphere.performance.controller.request.ReportRequest; import io.metersphere.performance.service.ReportService; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresRoles; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.OutputStream; import java.util.List; @RestController @@ -112,11 +111,15 @@ public class PerformanceReportController { } @GetMapping("log/download/{reportId}/{resourceId}") - public ResponseEntity downloadLog(@PathVariable String reportId, @PathVariable String resourceId) { - byte[] bytes = reportService.downloadLog(reportId, resourceId); - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"jmeter.log\"") - .body(bytes); + public void downloadLog(@PathVariable String reportId, @PathVariable String resourceId, HttpServletResponse response) throws Exception { + try (OutputStream outputStream = response.getOutputStream()) { + List content = reportService.downloadLog(reportId, resourceId); + response.setContentType("application/x-download"); + response.addHeader("Content-Disposition", "attachment;filename=jmeter.log"); + for (String log : content) { + outputStream.write(log.getBytes()); + } + outputStream.flush(); + } } } diff --git a/backend/src/main/java/io/metersphere/performance/service/ReportService.java b/backend/src/main/java/io/metersphere/performance/service/ReportService.java index 96bde66a53..ff5ec96f9e 100644 --- a/backend/src/main/java/io/metersphere/performance/service/ReportService.java +++ b/backend/src/main/java/io/metersphere/performance/service/ReportService.java @@ -25,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) @@ -210,14 +211,13 @@ public class ReportService { return loadTestReportLogMapper.selectByExampleWithBLOBs(example); } - public byte[] downloadLog(String reportId, String resourceId) { + public List downloadLog(String reportId, String resourceId) { LoadTestReportLogExample example = new LoadTestReportLogExample(); example.createCriteria().andReportIdEqualTo(reportId).andResourceIdEqualTo(resourceId); example.setOrderByClause("part desc"); List loadTestReportLogs = loadTestReportLogMapper.selectByExampleWithBLOBs(example); - String content = loadTestReportLogs.stream().map(LoadTestReportLog::getContent).reduce("", (a, b) -> a + b); - return content.getBytes(); + return loadTestReportLogs.stream().map(LoadTestReportLog::getContent).collect(Collectors.toList()); } public LoadTestReport getReport(String reportId) {