直接上代码
@RequestMapping(value = "/downVideoList", method = RequestMethod.GET)
@ResponseBody
public void downVideoList() throws Exception {
// 下载列表 查询阿里云id集合
List<vo> listFile = detailService.findListFile();
//阿里云视频集合
Vector<vo> downloadList = new Vector<HomeworkDetailFiles>();
// 遍历添加下载地址 将实体类换成自己的即可
for(HomeworkDetailFiles hdf:listFile){
DefaultProfile profile = DefaultProfile.getProfile(AliCloudParameter.VOD_DOMAIN, AliCloudParameter.access_key_id, AliCloudParameter.access_key_secret);
DefaultAcsClient client = new DefaultAcsClient(profile);
GetVideoInfoRequest req = new GetVideoInfoRequest();
req.setVideoId(阿里云id);
GetVideoInfoResponse res = null;
try{
res = client.getAcsResponse(req);
if(res.getVideo()!=null){
//根据阿里云id得到阿里云url
DefaultAcsClient client2 = initVodClient();
GetMezzanineInfoResponse response3 = new GetMezzanineInfoResponse();
if(response3!=null){
response3 = getMezzanineInfo(client2,阿里云id);
String fileUrl="";
fileUrl = response3.getMezzanine().getFileURL();
hdf.setVideoPath(阿里云路径);
downloadList.add(hdf);
}
}
}catch (Exception e){
e.printStackTrace();
continue;
}
}
download(downloadList);
}
/**
* 下载
*/
static void download(Vector<HomeworkDetailFiles> downloadList){
// 线程池
ExecutorService pool = null;
HttpURLConnection connection = null;
//循环下载
try {
for (int i = 0; i < downloadList.size(); i++) {
pool = Executors.newCachedThreadPool();
final String url = downloadList.get(i).getVideoPath();
String filename = getFilename(i+1+"_"+downloadList.get(i).getCompany()+"____"+downloadList.get(i).getUsername()+downloadList.get(i).getFileName());
System.out.println("正在下载第" + (i+1) + "个文件,地址:" + url);
Future<HttpURLConnection> future = pool.submit(new Callable<HttpURLConnection>(){
@Override
public HttpURLConnection call() throws Exception {
HttpURLConnection connection = null;
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(10000);//连接超时时间
connection.setReadTimeout(10000);// 读取超时时间
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
//断点续连,每次要算出range的范围,请参考Http 1.1协议
//connection.setRequestProperty("Range", "bytes=0");
connection.connect();
return connection;
}
});
connection = future.get();
System.out.println("下载完成.响应码:"+ connection.getResponseCode());
// 写入文件
String dir=i+1+"_"+downloadList.get(i).getCompany()+"____"+downloadList.get(i).getUsername();
writeFile(new BufferedInputStream(connection.getInputStream()), URLDecoder.decode(filename,"UTF-8"),dir);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != connection)
connection.disconnect();
if (null != pool)
pool.shutdown();
}
}
/**
* 通过截取URL地址获得文件名
* 注意:还有一种下载地址是没有文件后缀的,这个需要通过响应头中的
* Content-Disposition字段 获得filename,一般格式为:"attachment; filename=\xxx.exe\"
* @param url
* @return
*/
static String getFilename(String url){
return ("".equals(url) || null == url) ? "" : url.substring(url.lastIndexOf("/") + 1,url.length());
}
/**
* 写入文件
* @param inputStream
*/
static void writeFile(BufferedInputStream bufferedInputStream,String filename,String dir){
File dir2 = new File("G:\\temp\\download\\"+ dir);
// 判断目录是否存在
if (!dir2.exists()) {
dir2.mkdir();
}
//创建本地文件
File destfileFile = new File(dir2+"\\"+ filename);
if (destfileFile.exists()) {
destfileFile.delete();
}
if (!destfileFile.getParentFile().exists()) {
destfileFile.getParentFile().mkdir();
}
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(destfileFile);
byte[] b = new byte[1024];
int len = 0;
// 写入文件
System.out.println("开始写入本地文件.");
while ((len = bufferedInputStream.read(b, 0, b.length)) != -1) {
System.out.println("正在写入字节:" + len);
fileOutputStream.write(b, 0, len);
}
System.out.println("写入本地文件完成.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fileOutputStream) {
fileOutputStream.flush();
fileOutputStream.close();
}
if (null != bufferedInputStream)
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static DefaultAcsClient initVodClient() {
String VOD_DOMAIN = AliCloudParameter.VOD_DOMAIN;
DefaultProfile profile = DefaultProfile.getProfile(VOD_DOMAIN, AliCloudParameter.access_key_video_id, AliCloudParameter.access_key_video_secret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
public static GetMezzanineInfoResponse getMezzanineInfo(DefaultAcsClient client, String videoId) throws Exception {
GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
request.setVideoId(videoId);
//源片下载地址过期时间
request.setAuthTimeout(3600L);
return client.getAcsResponse(request);
}