解决方法:
String fileUrl = “http://u-api.xxxxx.com:32545/expose/2020/11/27/16dff157-cf7d-41c3-803a-7f92c54e095e.jpg”;
URL url = new URL(fileUrl);
InputStream stream = url.openStream();
解释:
当从本机上读取的时候例如:E:/2020/11/27/16dff157-cf7d-41c3-803a-7f92c54e095e.jpg,这个可以直接InputStream stream = new InputStream(“E:/2020/11/27/16dff157-cf7d-41c3-803a-7f92c54e095e.jpg”);
但是从远程上获取的路径则需要转成url格式才能解析。
相关代码:
public byte[] getBytesFromFile(File file,String url){
byte[] ret = null;
if (file == null) {
return null;
}
InputStream in = null;
ByteArrayOutputStream out = null;
//FileInputStream in = new FileInputStream(file);
try{
URL urlStr = new URL(url);
in = urlStr.openStream();
out = new ByteArrayOutputStream(4096);
byte[] b = new byte[4096];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
ret = out.toByteArray();
}catch(Exception e){
log.error("getBytesFromFile==Exception",e);
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
log.error("stream==Exception",e);
}
}
return ret;
}