Files
material-manage-service/src/test/java/mjkf/xinke/UploadFileTest.java
2024-07-08 16:39:40 +08:00

50 lines
1.9 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.util.ZipUtil;
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.text.MessageFormat;
@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\\材料管理系统模版\\主材\\网络价格.xlsx";
var file = new File(filePath);
var multipartFile = this.mockMultipartFile(file, "网络价格.xlsx", "application/x-zip-compressed");
var fileId = devFileService.uploadReturnId(DevFileEngineTypeEnum.LOCAL.getValue(), multipartFile);
System.out.println("UploadFileTest.test | fileId " + fileId);
}
private MultipartFile mockMultipartFile(File file, String fileName, String contentType) {
try {
// 使用文件内容和文件名创建MockMultipartFile
MockMultipartFile mockMultipartFile = new MockMultipartFile(
"file", // 参数名与表单提交时的name属性一致
fileName, // 文件名
contentType, // 文件内容类型
new FileInputStream(file) // 文件内容
);
return mockMultipartFile;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}