【采集调度器-后端HTTP API】下载采集器管理压缩包API

segment2.0
linrf 2 years ago
parent 00b6a5f453
commit dde18d75b0

@ -0,0 +1,31 @@
package com.docus.server.controller;
import com.docus.server.api.scheduling.management.FileApi;
import com.docus.server.service.IFileUploadService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
* API
*
* @author AutoGenerator
* @since 2023-07-15
*/
@RestController
public class FileController implements FileApi {
@Resource
private IFileUploadService iFileUploadService;
@Override
public void downloadFile(String filePath, HttpServletResponse response) throws Exception {
iFileUploadService.downloadFile(filePath, response);
}
@Override
public void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception {
iFileUploadService.uploadFile(multipartFiles, pathKey);
}
}

@ -10,6 +10,5 @@ public interface IFileUploadService {
List<UploadFileVO> uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception; List<UploadFileVO> uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception;
void downloadFile(HttpServletResponse response); void downloadFile(String filePath, HttpServletResponse response);
} }

@ -6,6 +6,7 @@ import com.docus.infrastructure.web.exception.ApiException;
import com.docus.infrastructure.web.exception.ExceptionCode; import com.docus.infrastructure.web.exception.ExceptionCode;
import com.docus.server.service.IFileUploadService; import com.docus.server.service.IFileUploadService;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO; import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -24,6 +25,7 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
@Service @Service
@Slf4j
public class FileUploadServiceImpl implements IFileUploadService { public class FileUploadServiceImpl implements IFileUploadService {
@Value("${file.uploadFolder:D://docus/}") @Value("${file.uploadFolder:D://docus/}")
@ -44,21 +46,10 @@ public class FileUploadServiceImpl implements IFileUploadService {
for (MultipartFile multipartFile : multipartFiles) { for (MultipartFile multipartFile : multipartFiles) {
if (multipartFile.isEmpty()) { valid(multipartFile);
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "上传失败,请选择文件!");
}
String originalFilename = multipartFile.getOriginalFilename();
if (StrUtil.isBlank(originalFilename)) {
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "未找到原文件文件名!");
}
if (!originalFilename.contains(".")) {
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "原文件名未识别到后缀!");
}
uploadFileVOList.add(actionUploadFile(multipartFile, pathKey)); uploadFileVOList.add(actionUploadFile(multipartFile, pathKey));
} }
return uploadFileVOList; return uploadFileVOList;
} }
@ -90,10 +81,11 @@ public class FileUploadServiceImpl implements IFileUploadService {
} }
@Override @Override
public void downloadFile(HttpServletResponse response) { public void downloadFile(String path, HttpServletResponse response) {
try { try {
//获取要下载的文件 //获取要下载的文件
File file = new File("file_path"); File file = new File(saveFilePath + "/" + path);
//设置响应的内容类型为二进制流,即文件类型 //设置响应的内容类型为二进制流,即文件类型
response.setContentType("application/octet-stream"); response.setContentType("application/octet-stream");
@ -124,9 +116,22 @@ public class FileUploadServiceImpl implements IFileUploadService {
buffInputStream.close(); buffInputStream.close();
inputStream.close(); inputStream.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(e.getMessage(), e);
} }
} }
private void valid(MultipartFile multipartFile) {
if (multipartFile.isEmpty()) {
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "上传失败,请选择文件!");
}
String originalFilename = multipartFile.getOriginalFilename();
if (StrUtil.isBlank(originalFilename)) {
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "未找到原文件文件名!");
}
if (!originalFilename.contains(".")) {
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "原文件名未识别到后缀!");
}
}
} }

@ -0,0 +1,39 @@
package com.docus.server.api.scheduling.management;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* API
*
* @author AutoGenerator
* @since 2023-07-15
*/
@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口")
@FeignClient(value = "collector-scheduling-management", contextId = "collector-scheduling-management.FileApi")
@RequestMapping("/sch/file")
public interface FileApi {
@ApiOperation("文件下载")
@GetMapping("/download")
void downloadFile(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws Exception;
@ApiOperation("文件上传")
@PostMapping("/upload")
@ApiImplicitParams({
@ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class)
})
void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception;
}

@ -0,0 +1,28 @@
package com.docus.server.dto.scheduling.management.schcollectorversionfile;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* AddDTO
*
* @author AutoGenerator
* @since 2023-07-15
*/
@Data
@ApiModel(value = "DownloadFileDTO", description = "DownloadFileDTO")
public class DownloadFileDTO implements Serializable {
@NotNull(message = "采集器文件路径不能为空")
@ApiModelProperty(value = "采集器文件路径")
private String filePath;
// @NotNull(message = "采集器文件保存路径不能为空")
// @ApiModelProperty(value = "采集器文件保存路径")
// private String savePath;
}
Loading…
Cancel
Save