2024-07-08 16:39:40 +08:00
|
|
|
|
package mjkf.xinke;
|
|
|
|
|
|
2024-12-10 17:35:47 +08:00
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
2024-07-08 16:39:40 +08:00
|
|
|
|
import mjkf.xinke.dev.modular.file.enums.DevFileEngineTypeEnum;
|
|
|
|
|
import mjkf.xinke.dev.modular.file.service.DevFileService;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.IOException;
|
2024-11-11 17:10:23 +08:00
|
|
|
|
import java.net.URLConnection;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
2024-07-08 16:39:40 +08:00
|
|
|
|
|
|
|
|
|
@SpringBootTest(classes = Application.class)
|
|
|
|
|
@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
|
|
public class UploadFileTest {
|
|
|
|
|
@Resource
|
|
|
|
|
DevFileService devFileService;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test() throws Exception {
|
2024-09-25 16:06:59 +08:00
|
|
|
|
// var filePath = "C:\\Users\\Administrator\\Desktop\\材料管理系统模版\\历史数据\\地材-202405.xlsx";
|
2024-12-25 10:00:36 +08:00
|
|
|
|
var filePath = "C:\\Users\\Administrator\\Documents\\对比材料精简9月.xlsx";
|
|
|
|
|
// var filePath = "C:\\Users\\Administrator\\Documents\\三明钢铁.xlsx";
|
2024-07-08 16:39:40 +08:00
|
|
|
|
var file = new File(filePath);
|
2024-11-11 17:10:23 +08:00
|
|
|
|
// var multipartFile = this.mockMultipartFile(file, "相邻城市价格.xlsx", "application/x-zip-compressed");
|
|
|
|
|
var multipartFile = this.mockMultipartFile(file);
|
2024-07-08 16:39:40 +08:00
|
|
|
|
var fileId = devFileService.uploadReturnId(DevFileEngineTypeEnum.LOCAL.getValue(), multipartFile);
|
|
|
|
|
System.out.println("UploadFileTest.test | fileId " + fileId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 09:53:43 +08:00
|
|
|
|
/**
|
|
|
|
|
* 1810213380969066497 网络价格.xlsx 1201
|
|
|
|
|
* 1810481654990442497 调查表.xlsx 501
|
|
|
|
|
* 1810481965629005826 公路局.xlsx 40201
|
|
|
|
|
* 1810482216079302658 交通局.xlsx 40101
|
2024-07-09 10:50:26 +08:00
|
|
|
|
* 1810497092512178177 地材.xlsx 1101
|
|
|
|
|
* 1810506191866003457 地材-202405.xlsx 1101
|
2024-12-25 10:00:36 +08:00
|
|
|
|
* 1871732061747179521 相邻城市.xlsx 600
|
2024-12-10 17:35:47 +08:00
|
|
|
|
* 1866412571689631746 三明钢铁.xlsx 301
|
2024-07-09 09:53:43 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2024-11-11 17:10:23 +08:00
|
|
|
|
private MultipartFile mockMultipartFile(File file) throws IOException {
|
|
|
|
|
var fileName = file.getName();
|
|
|
|
|
var contentType = Files.probeContentType(Path.of(file.getPath()));
|
2024-07-08 16:39:40 +08:00
|
|
|
|
try {
|
|
|
|
|
// 使用文件内容和文件名创建MockMultipartFile
|
|
|
|
|
MockMultipartFile mockMultipartFile = new MockMultipartFile(
|
|
|
|
|
"file", // 参数名,与表单提交时的name属性一致
|
|
|
|
|
fileName, // 文件名
|
|
|
|
|
contentType, // 文件内容类型
|
|
|
|
|
new FileInputStream(file) // 文件内容
|
|
|
|
|
);
|
|
|
|
|
return mockMultipartFile;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-11-11 17:10:23 +08:00
|
|
|
|
|
2024-07-08 16:39:40 +08:00
|
|
|
|
}
|