Java通过http协议下载文件并写入字节流的方法

3 篇文章 0 订阅
3 篇文章 0 订阅

 最近写微服务,从服务端通过http协议下载文件并写入字节流。开始以为很简单。直接上手就来了,代码如下:

 String szHttpFile="http://..../downlaod&&fileId="+fileId+"&&extra[action]=download&extra[ext]="+extName;
 try {
     HttpURLConnection connection = (HttpURLConnection) new URL(szHttpFile).openConnection();
     connection.setRequestMethod("GET");

     int code = connection.getResponseCode();
     if (code == 200 || code == 206) {
         int contentLength = connection.getContentLength();
         InputStream is = connection.getInputStream();
         is.read(btData);
         is.close();
     }
 } catch (IOException e) {
     ...

      简单测试后,应用正常,以为没有问题。就提交了。过了几天用,大文件测试,发现不稳定,经常丢失字节流。而且不稳定。郁闷。后面研究InputStream .read()代码,发现了问题,在文件较大时,有拼接字符信息。所以read时,不一定能读取文件内容。需要采用以下方法才可以:

 String szHttpFile="http://..../downlaod&&fileId="+fileId+"&&extra[action]=download&extra[ext]="+extName;
 try {
     HttpURLConnection connection = (HttpURLConnection) new URL(szHttpFile).openConnection();
     connection.setRequestMethod("GET");

     int code = connection.getResponseCode();
     if (code == 200 || code == 206) {
         int contentLength = connection.getContentLength();
         InputStream is = connection.getInputStream();
         DataInputStream dataInputStream = new DataInputStream(is);
         dataInputStream.readFully(btData);
         dataInputStream.close();
         is.close();
     }
 } catch (IOException e) {
...
 }
注意黑色加粗部分,是完全读取并处理的。后面发布和大数据测试正常。记录下来防止自己再犯类似错误,同时,也给遇到类似问题的朋友提供点解决问题的方法,相互学习交流和切磋。
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
您好,可以使用Java中的HttpClient库和Jsoup库来实现爬取视频并下载到本地的功能。具体实现步骤如下: 1. 使用HttpClient库发送HTTP请求,获取视频的URL地址。 2. 使用Jsoup库解析HTML页面,获取视频的URL地址。 3. 使用Java中的IO将视频文件下载到本地。 以下是示例代码: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class VideoDownloader { public static void main(String[] args) throws Exception { String url = "https://www.example.com/video.html"; String videoUrl = getVideoUrl(url); downloadVideo(videoUrl, "video.mp4"); } public static String getVideoUrl(String url) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); Document document = Jsoup.parse(inputStream, "UTF-8", url); Element videoElement = document.select("video").first(); String videoUrl = videoElement.attr("src"); response.close(); return videoUrl; } public static void downloadVideo(String videoUrl, String fileName) throws Exception { URL url = new URL(videoUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); InputStream inputStream = connection.getInputStream(); FileOutputStream outputStream = new FileOutputStream(fileName); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.close(); inputStream.close(); } } ``` 请注意,此代码仅供参考,具体实现可能因网站结构和视频格式而异。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

余山水

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

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

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

打赏作者

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

抵扣说明:

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

余额充值