Files
material-manage-service/src/test/java/mjkf/xinke/UploadFileTest.java
2024-12-10 17:35:47 +08:00

69 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mjkf.xinke;
import cn.hutool.core.io.FileUtil;
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;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class UploadFileTest {
@Resource
DevFileService devFileService;
@Test
public void test() throws Exception {
// var filePath = "C:\\Users\\Administrator\\Desktop\\材料管理系统模版\\历史数据\\地材-202405.xlsx";
// var filePath = "C:\\Users\\Administrator\\Documents\\工作表 在 9月完成功能.xlsx";
var filePath = "C:\\Users\\Administrator\\Documents\\三明钢铁.xlsx";
var file = new File(filePath);
// var multipartFile = this.mockMultipartFile(file, "相邻城市价格.xlsx", "application/x-zip-compressed");
var multipartFile = this.mockMultipartFile(file);
var fileId = devFileService.uploadReturnId(DevFileEngineTypeEnum.LOCAL.getValue(), multipartFile);
System.out.println("UploadFileTest.test | fileId " + fileId);
}
/**
* 1810213380969066497 网络价格.xlsx 1201
* 1810481654990442497 调查表.xlsx 501
* 1810481965629005826 公路局.xlsx 40201
* 1810482216079302658 交通局.xlsx 40101
* 1810497092512178177 地材.xlsx 1101
* 1810506191866003457 地材-202405.xlsx 1101
* 1838851336371163138 相邻城市.xlsx 600
* 1866412571689631746 三明钢铁.xlsx 301
*/
private MultipartFile mockMultipartFile(File file) throws IOException {
var fileName = file.getName();
var contentType = Files.probeContentType(Path.of(file.getPath()));
try {
// 使用文件内容和文件名创建MockMultipartFile
MockMultipartFile mockMultipartFile = new MockMultipartFile(
"file", // 参数名与表单提交时的name属性一致
fileName, // 文件名
contentType, // 文件内容类型
new FileInputStream(file) // 文件内容
);
return mockMultipartFile;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}