故障录波文件下载

This commit is contained in:
huguanghan 2024-11-14 14:50:12 +08:00
parent 0418feb20a
commit 0947eef1f7
5 changed files with 76 additions and 17 deletions

View File

@ -5,7 +5,9 @@ import com.alibaba.fastjson.JSONObject;
import com.das.common.result.R;
import com.das.modules.equipment.entity.SysEquipment;
import com.das.modules.fdr.domain.FileNode;
import com.das.modules.fdr.domain.dto.FileDownloadDto;
import com.das.modules.fdr.service.FaultRecorderService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -45,4 +47,10 @@ public class FaultRecorderController {
faultRecorderService.updateFdrConfig(sysEquipment);
}
@RequestMapping(value = "/download", method = RequestMethod.POST)
public void downloadFdrFile(@RequestBody FileDownloadDto fileDownloadDto, HttpServletResponse httpServletResponse) throws IOException {
faultRecorderService.download(fileDownloadDto.getUrl(),httpServletResponse);
}
}

View File

@ -0,0 +1,13 @@
package com.das.modules.fdr.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FileDownloadDto {
private String url;
}

View File

@ -2,6 +2,7 @@ package com.das.modules.fdr.service;
import com.das.modules.equipment.entity.SysEquipment;
import com.das.modules.fdr.domain.FileNode;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@ -13,6 +14,8 @@ import java.util.Map;
public interface FaultRecorderService {
List<FileNode> getDirOrFileList(String name,String startTime, String endTime);
void download(String path, HttpServletResponse httpServletResponse) throws IOException;
Map<String, List<Object>> getDataCurve(String url, String deviceCode) throws IOException;
void updateFdrConfig(SysEquipment sysEquipment);
@ -20,9 +23,4 @@ public interface FaultRecorderService {
String upload(String parent, String folderName, MultipartFile file);
void readFileToSteam(String path, OutputStream stream);
void download(String path, Path tempDir);
}

View File

@ -8,6 +8,7 @@ import io.micrometer.common.util.StringUtils;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Item;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -185,22 +186,60 @@ public class MinioViewsServcie {
}
}
public void download(String path, Path tempDir) {
public void download(String path, Path tempDir,HttpServletResponse httpServletResponse) {
File tempFile = null;
try (InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
.bucket(minioProperties.getBucket())
.object(path)
.build())) {
// 保存到临时文件夹
File tempFile = tempDir.resolve(tempDir+path).toFile();
FileUtil.writeFromStream(inputStream,tempFile);
}
catch (Exception ignored){
tempFile = tempDir.resolve(tempDir + path).toFile();
FileUtil.writeFromStream(inputStream, tempFile);
writeFileToRes(tempFile, httpServletResponse);
} catch (Exception ignored) {
} finally {
assert tempFile != null;
if (tempFile.exists()){
tempFile.delete();
}
}
}
public void writeFileToRes(File downloadFile, HttpServletResponse response) throws IOException {
// 检查文件是否存在
if (!downloadFile.exists()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 获取文件名
String fileName = downloadFile.getName();
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
// 设置响应头
response.setContentType("application/octet-stream");
response.setContentLength((int) downloadFile.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 读取文件并写入响应的输出流
try (FileInputStream inStream = new FileInputStream(downloadFile);
OutputStream outStream = response.getOutputStream()) {
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
}
// 递归方式 计算文件的大小
public long getTotalSizeOfFilesInDir(File file) {
if (file.isFile()) {
@ -229,4 +268,5 @@ public class MinioViewsServcie {
return inputStream;
}
}

View File

@ -1,7 +1,6 @@
package com.das.modules.fdr.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.das.common.constant.FileConstants;
import com.das.common.exceptions.ServiceException;
@ -12,17 +11,16 @@ import com.das.modules.fdr.domain.FileNode;
import com.das.modules.fdr.domain.vo.FdrFormatVo;
import com.das.modules.fdr.service.FaultRecorderService;
import com.das.modules.fdr.service.MinioViewsServcie;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micrometer.common.util.StringUtils;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.rmi.ServerException;
import java.text.ParseException;
@ -30,7 +28,6 @@ import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Slf4j
@ -75,8 +72,9 @@ public class FaultRecorderServiceImpl implements FaultRecorderService {
}
@Override
public void download(String path, Path tempDir) {
minioViewsServcie.download(path, tempDir);
public void download(String path,HttpServletResponse httpServletResponse) throws IOException {
Path tempDir = Files.createTempDirectory(null);
minioViewsServcie.download(path, tempDir,httpServletResponse);
}
@ -188,6 +186,8 @@ public class FaultRecorderServiceImpl implements FaultRecorderService {
return map;
}
public long convertToTimestamp(String time, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(time).getTime();