简体   繁体   English

将不在类路径中的文件加载到 Spring 资源中?

[英]Loading a file not in the classpath into a Spring Resource?

I currently have a Spring API service that takes a Base64 String img and decodes it.我目前有一个 Spring API 服务,它接受一个 Base64 字符串 img 并对其进行解码。

String imageDataBytes = img.substring(img.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decodeBase64(imageDataBytes));

I want to load contents of stream onto a Resource class.我想将流的内容加载到 Resource 类上。 But so far my research is telling me I can only load resources through a URL or file path, which I don't have here.但到目前为止,我的研究告诉我我只能通过 URL 或文件路径加载资源,而我在这里没有。 What can I do?我能做什么?

You can use ByteArrayResource.您可以使用 ByteArrayResource。 It takes a byte array in constructor.它在构造函数中接受一个字节数组。 Here is the documentation .这是文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM