java远程文件下载到本地
适用场景:我们再登录网站后选择播放音频,如果需要将音频或者视频下载到本地时,而网站资源又进行了加密,无法获得的情况下。使用该工具类模拟浏览器访问下载
package com.battcn.platform.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Name: RemoteFile.java
* @Description: Java下载远程文件到本地。
* @Author: PeiFeng
* @Create Date: 2017-8-12
*/
public class downRemoteFile {
/**
* @Name: downRemoteFile。
* @Description: 下载远程文件。
* @Parameters: remoteFileUrl,要下载的远程文件地址。
* @Parameters: saveFileName,下载后保存的文件名。
* @Parameters: saveDir,下载后保存的文件路径。
* @Return: String,文件保存的地址。
* @Author: PeiFeng
* @Version: V1.00
* @Create Date: 2017-8-12
*/
public static String RemoteFile( String remoteFileUrl, String saveFileName, String saveDir) {
HttpURLConnection conn = null;
OutputStream oputstream = null;
InputStream iputstream = null;
try {
// 创建保存文件的目录
File savePath = new File(saveDir);
if (!savePath.exists()) {
savePath.mkdir();
}
// 创建保存的文件
File file = new File(savePath + "/" + saveFileName);
if (file != null && !file.exists()) {
file.createNewFile();
}
URL url = new URL(remoteFileUrl);
// 将url以open方法返回的urlConnection连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接)
// 此时cnnection只是为一个连接对象,待连接中
conn = (HttpURLConnection) url.openConnection();
// 示例:下方的请求头以火狐浏览器为例,如果使用别的浏览器则需要修改请求头
conn.setRequestProperty("Accept","*/*");
conn.setRequestProperty("Accept-Encoding","gzip, deflate, br");
conn.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
conn.setRequestProperty("Connection","keep-alive");
conn.setRequestProperty("Host","audiblecdns3prod-vh.akamaihd.net");
conn.setRequestProperty("Origin","https://stories.audible.com");
// Referer跟的value值为使用浏览器访问资源的网址
conn.setRequestProperty("Referer","https://stories.audible.com/pdp/B07SZHS822?ref=adbl_ent_anon_ds_pdp_pc_pg-1-cntr-0-16");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
// 设置是否要从 URL连接读取数据,默认为true
conn.setDoInput(true);
// 建立连接
// (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
conn.connect();
// 连接发起请求,处理服务器响应 (从连接获取到输入流)
iputstream = conn.getInputStream();
// 创建文件输出流,用于保存下载的远程文件
oputstream = new FileOutputStream(file);
// 用来存储响应数据
byte[] buffer = new byte[4 * 1024];
int byteRead = -1;
// 循环读取流
while ((byteRead = (iputstream.read(buffer))) != -1) {
oputstream.write(buffer, 0, byteRead);
}
// 输出完成后刷新并关闭流
oputstream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 重要且易忽略步骤 (关闭流,切记!)
if (iputstream != null) {
iputstream.close();
}
if (oputstream != null) {
oputstream.close();
}
// 销毁连接
if (conn != null) {
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回保存后的文件路径
return saveDir + "/" + saveFileName;
}
public static void main(String[] args) throws Exception {
// x小于的值则为资源加载的时候第一个index_0_a.m3u8文件的响应.ts文件的个数
// RemoteFile()第一项为要下载的远程文件地址,需要将segment"+x+"_0_a.ts换成循环值,以方便下载
// 后边的即为下载被切割资源的.ts文件保存地址。
for (int x=1;x<=4448;x++){
RemoteFile("https://audiblecdns3prod-vh.akamaihd.net/i/295894/audiblewords/content/bk/adbj/000047/V$172537$V/aax/bk_adbj_000047_22_32.mp4/segment"+x+"_0_a.ts?null=0&hdntl=exp=1585635931~acl=/i/295894/audiblewords/content/bk/adbj/000047/V$172537$V/aax/bk_adbj_000047_22_32.mp4/*~data=hdntl~hmac=2e8450196f80e59966a286b501ecfd7f82ce9577ebceaae2c6b8edf258de873c",
"segment"+x+"_0_a.ts", "D:/Downloads/文明論之概略");
}
}
}
注意:查看网站中的一些文件需要在网站中点击F12进行开发者模式,在网络处查看返回的个数以访问的远程文件的地址