有没有办法将File对象转换为multipartfile?所以我可以将该对象发送给接受multipartfile接口的对象的方法?
File myFile = new File("/path/to/the/file.txt") multipartfile ....? def (multipartfile file) { def is = new BufferedInputStream(file.getInputStream()) //do something interesting with the stream }
解决方法
为此存在
MockMultipartFile.如果您的代码段中的文件路径已知,则以下代码适用于我.
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import org.springframework.mock.web.Mockmultipartfile; Path path = Paths.get("/path/to/the/file.txt"); String name = "file.txt"; String originalFileName = "file.txt"; String contentType = "text/plain"; byte[] content = null; try { content = Files.readAllBytes(path); } catch (final IOException e) { } multipartfile result = new Mockmultipartfile(name,originalFileName,contentType,content);
原文地址:https://www.jb51.cc/java/122417.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。