completed binary file download webservice

This commit is contained in:
strawmanbobi
2019-06-25 22:57:29 +08:00
parent c650b0272e
commit 5c6d46f04d
2 changed files with 27 additions and 5 deletions

View File

@@ -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<RemoteIndex> 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<RemoteIndex> 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);
}
}

View File

@@ -85,15 +85,20 @@ public class IROperationService extends AbstractBaseService {
public ResponseEntity<InputStreamResource> 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);
}