江西住房城乡建设网站搜索广告是什么
江西住房城乡建设网站,搜索广告是什么,wordpress实时预览载入中,百度站长平台开绿色收录通道加快网站收录假如你把你的后端项目部署在服务器上的时候#xff0c;然后你要去读取某个路径下的文件#xff0c;然后你就要提供文件的路径#xff0c;然后获取到该文件对象#xff0c;但是你需要将当前文件对象File转换成MultipartFile再发送http请求传递到其他服务器上#xff0c;这样… 假如你把你的后端项目部署在服务器上的时候然后你要去读取某个路径下的文件然后你就要提供文件的路径然后获取到该文件对象但是你需要将当前文件对象File转换成MultipartFile再发送http请求传递到其他服务器上这样如何实现呢 后端如何发送http请求请参考【Java】Java发送httpPosthttpGethttpDelete请求-CSDN博客
代码实现
import org.springframework.web.multipart.MultipartFile;
import java.io.*;public class FileToMultipartFile implements MultipartFile {private final File file;public FileToMultipartFile(File file) {this.file file;}Overridepublic String getName() {return file.getName();}Overridepublic String getOriginalFilename() {return file.getName();}Overridepublic String getContentType() {return application/octet-stream;}Overridepublic boolean isEmpty() {return file.length() 0;}Overridepublic long getSize() {return file.length();}Overridepublic byte[] getBytes() throws IOException {InputStream is new FileInputStream(file);ByteArrayOutputStream baos new ByteArrayOutputStream();byte[] buffer new byte[1024];int len;while ((len is.read(buffer)) ! -1) {baos.write(buffer, 0, len);}return baos.toByteArray();}Overridepublic InputStream getInputStream() throws IOException {return new FileInputStream(file);}Overridepublic void transferTo(File dest) throws IOException, IllegalStateException {try (InputStream is new FileInputStream(file);OutputStream os new FileOutputStream(dest)) {byte[] buffer new byte[1024];int len;while ((len is.read(buffer)) ! -1) {os.write(buffer, 0, len);}}}
}单元测试
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockMultipartFile;public class FileToMultipartFileTest {private FileToMultipartFile fileToMultipartFile;BeforeEachpublic void setUp() {// 在测试之前设置 FileToMultipartFile 实例// 假设存在一个名为 xxx.png 的文件File file new File(static/images/xxx.png);fileToMultipartFile new FileToMultipartFile(file);}Testpublic void testGetName() {// 测试 getName 方法String name fileToMultipartFile.getName();assertEquals(default_avatar.png, name);}Testpublic void testGetOriginalFilename() {// 测试 getOriginalFilename 方法String originalFilename fileToMultipartFile.getOriginalFilename();assertEquals(default_avatar.png, originalFilename);}Testpublic void testGetContentType() {// 测试 getContentType 方法String contentType fileToMultipartFile.getContentType();assertEquals(application/octet-stream, contentType);}Testpublic void testIsEmpty() {// 测试 isEmpty 方法boolean isEmpty fileToMultipartFile.isEmpty();assertTrue(!isEmpty); // 这里应该为 false}Testpublic void testGetSize() {// 测试 getSize 方法long size fileToMultipartFile.getSize();assertTrue(size 0); // 文件大小应该大于 0}// 其它方法的测试可以类似地进行
}如何你需要转换直接引入上面的FileToMultipartFile即可。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/86338.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!