/**
* 保存到本地的工具类
* @param args
*/
public static void main(String[] args) {
String txUrl = "https://video.com/372900665-1-16.mp4?e=i";
// 生成视频名称
String spName = System.currentTimeMillis() + ".mp4";
// win系统的自行更换
String bdPath = "E:\\shiping/"+spName; // 保存到服务器的地址
boolean downVideo = downVideo(txUrl, bdPath);
}
/**
* 下载视频
* @param videoUrl 视频网络地址
* @param downloadPath 视频保存地址
*/
public static boolean downVideo(String videoUrl, String downloadPath) {
HttpURLConnection connection = null;
InputStream inputStream = null;
RandomAccessFile randomAccessFile = null;
boolean re;
try {
URL url = new URL(videoUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Range", "bytes=0-");
connection.connect();
if (connection.getResponseCode() / 100 != 2) {
System.out.println("连接失败...");
return false;
}
inputStream = connection.getInputStream();
int downloaded = 0;
int fileSize = connection.getContentLength();
randomAccessFile = new RandomAccessFile(downloadPath, "rw");
while (downloaded < fileSize) {
byte[] buffer = null;
if (fileSize - downloaded >= 1000000) {
buffer = new byte[1000000];
} else {
buffer = new byte[fileSize - downloaded];
}
int read = -1;
int currentDownload = 0;
long startTime = System.currentTimeMillis();
while (currentDownload < buffer.length) {
read = inputStream.read();
buffer[currentDownload++] = (byte) read;
}
long endTime = System.currentTimeMillis();
double speed = 0.0;
if (endTime - startTime > 0) {
speed = currentDownload / 1024.0 / ((double) (endTime - startTime) / 1000);
}
randomAccessFile.write(buffer);
downloaded += currentDownload;
randomAccessFile.seek(downloaded);
System.out.printf(downloadPath+"下载了进度:%.2f%%,下载速度:%.1fkb/s(%.1fM/s)%n", downloaded * 1.0 / fileSize * 10000 / 100,
speed, speed / 1000);
}
re = true;
return re;
} catch (MalformedURLException e) {
e.printStackTrace();
re = false;
return re;
} catch (IOException e) {
e.printStackTrace();
re = false;
return re;
} finally {
try {
connection.disconnect();
inputStream.close();
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【java 通过视频url下载】
最新推荐文章于 2024-06-09 00:35:01 发布