java如何下载视频到本地
作者:OpenWorld
时间:2023-05-08
来源:正软商城
浏览:6
1.加载servlet容器不能使用main方法直接调用publicstaticbooleanhttpDownload(StringhttpUrl,StringsaveFile){//1.下载网络文件intbyteRead;URLurl;try{url=newURL(httpUrl);}catch(MalformedURLExceptione1){e1.printStackTrace();returnfalse;}try{//2.获取链接URLConnectionconn=url.openConnectio
本文目录
1.加载servlet容器
不能使用main方法直接调用
public static boolean httpDownload(String httpUrl, String saveFile) {
// 1.下载网络文件
int byteRead;
URL url;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e1) {
e1.printStackTrace();
return false;
}
try {
//2.获取链接
URLConnection conn = url.openConnection();
//3.输入流
InputStream inStream = conn.getInputStream();
//3.写入文件
FileOutputStream fs = new FileOutputStream(saveFile);
byte[] buffer = new byte[1024];
while ((byteRead = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteRead);
}
inStream.close();
fs.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Test
public void httpDownload() {
httpDownload("http://video.zhihuishu.com/zhs/ablecommons/demo/201806/dddee1c446314b84a26c74a8def3c3c7.mp4","E:\\test/22.mp4");
}2.添加common-io依赖
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
public class TestDownloadFile {
public static void main(String[] args) throws Exception{
String urlStr = "https://img2018.cnblogs.com/i-beta/1278703/201911/1278703-20191128121650595-812419505.png";
URL url = new URL(urlStr);
String tempFileName = "E://a.png";
File temp = new File(tempFileName);
FileUtils.copyURLToFile(url, temp);
}
}
作者最新文章
VisualStudio2019安装完成重置开发环境默认配置
2026-11-06 09:58
PDF图片太多怎么批量压缩?PDF文档如何压缩变小?
2026-10-27 11:48
花4700元买RTX 5070:收到却是一堆20年前的电子垃圾!
2026-10-26 16:25
Aperty人像精修指南:去除面部红晕斑点与调整阴影
2026-10-22 21:53
即梦AI怎么导出到剪映继续剪辑?
2026-10-22 19:49
热门文章
更多
精品专题
更多
Mac软件
更多
火山引擎 网页版
Windows/ macOS/ Linux
火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。
















