要下载视频到本地,可以使用Java的URL类和HttpURLConnection类来实现。是一个简单的例子:
import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.net.HttpURLConnection; public class DownloadVideo { public static void main(String[] args) { String videoUrl = "http://example.com/video.mp4"; try { URL url = new URL(videoUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); FileOutputStream outputStream = new FileOutputStream("video.mp4"); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = connection.getInputStream().read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); connection.disconnect(); System.out.println("Video downloaded"); } catch (IOException ex) { ex.printStackTrace(); } } }
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(13条)