From 5c6d46f04d7eb204b6a31a3a724961c6514447f9 Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Tue, 25 Jun 2019 22:57:29 +0800 Subject: [PATCH] completed binary file download webservice --- .../service/businesslogic/IndexingLogic.java | 17 +++++++++++++++++ .../service/restapi/IROperationService.java | 15 ++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/irext/server/service/businesslogic/IndexingLogic.java b/src/main/java/net/irext/server/service/businesslogic/IndexingLogic.java index b942380..418473a 100644 --- a/src/main/java/net/irext/server/service/businesslogic/IndexingLogic.java +++ b/src/main/java/net/irext/server/service/businesslogic/IndexingLogic.java @@ -6,6 +6,7 @@ import net.irext.server.service.model.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import java.io.File; import java.util.List; /** @@ -36,6 +37,9 @@ public class IndexingLogic { @Autowired private RemoteIndexMapper remoteIndexMapper; + private static final String IR_BIN_FILE_PREFIX = "irda_"; + private static final String IR_BIN_FILE_SUFFIX = ".bin"; + public RemoteIndex getRemoteIndex(int indexId) { List remoteIndexList = remoteIndexMapper.getRemoteIndexById(indexId); if (null != remoteIndexList && remoteIndexList.size() > 0) { @@ -93,4 +97,17 @@ public class IndexingLogic { } return remoteIndexList; } + + public File getDownloadStream(int remoteIndexId) { + List remoteIndexList = remoteIndexMapper.getRemoteIndexById(remoteIndexId); + if (null == remoteIndexList || 0 == remoteIndexList.size()) { + return null; + } + + RemoteIndex remote = remoteIndexList.get(0); + String downloadPath = "/data/irext/"; + String fileName = IR_BIN_FILE_PREFIX + remote.getRemoteMap() + IR_BIN_FILE_SUFFIX; + String localFilePath = downloadPath + fileName; + return new File(localFilePath); + } } diff --git a/src/main/java/net/irext/server/service/restapi/IROperationService.java b/src/main/java/net/irext/server/service/restapi/IROperationService.java index c45b9e9..1cda7c8 100644 --- a/src/main/java/net/irext/server/service/restapi/IROperationService.java +++ b/src/main/java/net/irext/server/service/restapi/IROperationService.java @@ -85,15 +85,20 @@ public class IROperationService extends AbstractBaseService { public ResponseEntity downloadBin( @RequestBody DownloadBinaryRequest downloadBinaryRequest) throws IOException { - FileInputStream inputStream = indexingLogic.getDownloadStream(downloadBinaryRequest.getIndexId()); - InputStreamResource resource = new InputStreamResource(inputStream); - String fileName = ""; - int fileLength = 0; + File downloadFile = indexingLogic.getDownloadStream(downloadBinaryRequest.getIndexId()); + + if (null == downloadFile) { + return ResponseEntity.ok().body(null); + } + + InputStreamResource resource = new InputStreamResource(new FileInputStream(downloadFile)); + String fileName = downloadFile.getName(); + long fileLength = downloadFile.length(); return ResponseEntity.ok() // Content-Disposition .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName) - // Contet-Length + // Content-Length .contentLength(fileLength) .body(resource); }