InputStream读取远程url时读不出来,报(文件名、目录名或卷标语法不正确)的错

32 篇文章 0 订阅

解决方法:

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; 
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

゛Smlie。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值