OrderServiceTest.java
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.bs.order;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.domain.FileEntity;
import com.bs.fileservice.feign.RemoteFileService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import java.io.File;
import java.nio.file.Files;
@Slf4j
@SpringBootTest
@ActiveProfiles("local")//测试环境
class OrderServiceTest {
@Autowired
private Environment environment;
@Autowired
private RemoteFileService remoteFileService;
@Test
void testFileUpload() {
// 本地待上传文件路径,请按需修改
String localFilePath = "C:\\Users\\Administrator\\Pictures\\bb9214827d76c5c26d21e273c5868721.jpg";
File localFile = new File(localFilePath);
if (!localFile.exists()) {
log.error("本地文件不存在:{}", localFilePath);
return;
}
try {
// 将本地文件转换为 MultipartFile
String originalFilename = localFile.getName();
String contentType = Files.probeContentType(localFile.toPath());
if (contentType == null) {
contentType = "application/octet-stream";
}
MockMultipartFile multipartFile = new MockMultipartFile(
"file", // 表单字段名,需与服务端 @RequestPart("file") 一致
originalFilename, // 原始文件名
contentType, // Content-Type
Files.readAllBytes(localFile.toPath())
);
log.info("开始通过 Feign 调用文件服务上传文件: {}", localFilePath);
AjaxResult<FileEntity> result = remoteFileService.upload(multipartFile);
log.info("上传结果: {}", result);
if (result != null && result.getData() != null) {
log.info("文件名: {}", result.getData().getName());
log.info("文件地址: {}", result.getData().getUrl());
}
} catch (Exception e) {
log.error("Feign 调用文件上传失败", e);
}
}
}