java读取远程图片缺失或者不全

参考博客:https://blog.csdn.net/changerzhuo_319/article/details/70161387
读取远程图片时,读不全,图片只读到一部分,怎么回事呢?
这就是网络上读取时丢包了。
怎么样才能读取完整的图片呢?
其实很简单,控制好读完才停止。怎么确定读完了呢。连接有个方法获取远程资源的长度–getContentLength()
具体代码如下:

public static String urlToString(String imgUrl){
        byte[] result=null;
        InputStream inStream=null;
        String photo = "";
        try {
            //创建URL
            URL url=new URL(imgUrl);
            //创建连接
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5*1000);
            inStream=conn.getInputStream();
            int count=conn.getContentLength();//获取远程资源长度
            result=new byte[count];
            int readCount=0;
            while(readCount<count){//循环读取数据
                readCount+=inStream.read(result,readCount,count-readCount);
            }
            photo = toBase64(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取远程图片失败", e);
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }
            } catch (IOException e) {
                log.error("文件处理错误!", e);
            }
            log.info("o==||=====>读取图片返回");
        }
        return photo;
    }
    private static String toBase64(byte[] imgData) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(imgData);
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java可以使用URLConnection类或者Apache HttpClient库来读取远程服务器的文件列表。 1. 使用URLConnection类: 首先,创建一个URL对象,指定要访问的远程服务器地址。然后,使用URL对象的openConnection()方法打开一个连接,并转换为HttpURLConnection对象。接着,设置HTTP请求的方法(GET、POST等)、超时时间等属性,并连接到远程服务器。通过getInputStream()方法获取远程服务器的响应数据流,并使用BufferedReader逐行读取文件列表。 以下是示例代码: ``` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class RemoteFileReader { public static void main(String[] args) { try { URL url = new URL("https://example.com/filelist"); // 替换为实际的远程服务器地址 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); // 设置连接超时时间为5秒 connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); // 输出远程服务器的文件列表 } reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 2. 使用Apache HttpClient库: 首先,引入Apache HttpClient库的相关依赖。然后,创建一个HttpClient对象,并使用HttpGet请求指定的远程服务器地址。通过调用execute()方法发送请求并获取响应。使用EntityUtils类将响应的实体转换为字符串,并解析获取文件列表信息。 以下是示例代码: ``` import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class RemoteFileReader { public static void main(String[] args) { try { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://example.com/filelist"); // 替换为实际的远程服务器地址 HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String filelist = EntityUtils.toString(entity); // 获取文件列表字符串 System.out.println(filelist); // 输出远程服务器的文件列表 httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 无论是使用URLConnection类还是Apache HttpClient库,需要替换示例代码中的远程服务器地址为实际的地址。在实际应用中,可能需要根据远程服务器的访问授权、协议、端口等设置相关参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值