JKXY的视频内容下载工具类
package cn.jsonlu.make.license; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List; /**
* Author:JsonLu
* DateTime:16/7/28 09:23
* Email:jsonlu@qq.com
* Desc:
*/
public class FileUpload { private List<File> list = new ArrayList();
private List<String> urls = new ArrayList<String>();
private final String path = "D:\\res\\cordova"; @Test
public void test() throws Exception {
File root = new File(path);
getAllFiles(root);
for (File file : list) {
readTxtFile(file);
}
for (String url : urls) {
int len = url.split("/").length;
String name = url.split("/")[len - 1];
DownloadMP4 downloadMP4 = new DownloadMP4();
downloadMP4.downLoadFromUrl(url, name, path);
} } /**
* 读取目录下的文件内容
*
* @param file
*/
private void readTxtFile(File file) {
try {
String encoding = "UTF-8";
if (file.isFile() && file.exists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
JSONObject object = JSON.parseObject(lineTxt);
JSONObject u = object.getJSONObject("data");
String url = u.getString("url");
urls.add(url);
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
} } /**
* 遍历目录下的文件
*
* @param dir
* @throws Exception
*/
private void getAllFiles(File dir) throws Exception {
File[] fs = dir.listFiles();
for (int i = 0; i < fs.length; i++) {
list.add(fs[i].getAbsoluteFile());
if (fs[i].isDirectory()) {
try {
getAllFiles(fs[i]);
} catch (Exception e) {
}
}
}
} class DownloadMP4 { /**
* 从网络Url中下载文件
*
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public void downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; HUAWEI NXT-DL00 Build/HUAWEINXT-DL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.2 TBS/036523 Safari/537.36 V1_AND_SQ_6.3.7_374_YYB_D QQ/6.3.7.2795 NetType/WIFI WebP/0.3.0 Pixel/1080");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream); //文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
System.out.println("download success:" + url);
} /**
* 从输入流中获取字节数组
*
* @param inputStream
* @return
* @throws IOException
*/
private byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
} }
JKXY的视频内容下载工具类的更多相关文章
- 今日头条、抖音、西瓜、火山、微视、陌陌等自媒体平台小视频批量下载工具v1.1.0(视频搬运福利)
前言 目前各大自媒体平台爆火,网络流量暴涨,各大自媒体平台的小视频为广大个广告主带来了如泉涌般的的视频流量,更给广大的自媒体小编带来了丰厚的利益回报,想要创做更多的自媒体内容着实不易,下面给广大的小视 ...
- ftp上传下载工具类
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...
- 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)
前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...
- Spring MVC文件上传下载工具类
import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...
- 用Java编写的http下载工具类,包含下载进度回调
HttpDownloader.java package com.buyishi; import java.io.FileOutputStream; import java.io.IOException ...
- HttpServletRequest内容处理工具类
目录 HttpServletRequestUtil类 (可转换成json数据,xml数据,map数据) HttpServletRequestUtil类 import javax.servlet.htt ...
- SFTP 文件上传下载工具类
SFTPUtils.java import com.jcraft.jsch.*; import com.jcraft.jsch.ChannelSftp.LsEntry; import lombok.e ...
- android 数据下载 工具类
传入图片地址,获得服务器返回的流. 把流转化为byte[]数组
- python实现的视频下载工具you-get,支持多个国内外主流视频平台
RT,you-get 是一个视频离线下载工具, https://github.com/soimort/you-get 另一个同类工具 youtube-dl 也是python 实现,虽然名为 youtu ...
随机推荐
- MySQL源码 数据结构hash
MySQL源码自定义了hash表,因为hash表具有O(1)的查询效率,所以,源码中大量使用了hash结构.下面就来看下hash表的定义: [源代码文件include/hash.h mysys/has ...
- Apache mod_fcgid fcgid_header_bucket_read函数缓冲区溢出漏洞
漏洞名称: Apache mod_fcgid fcgid_header_bucket_read函数缓冲区溢出漏洞 CNNVD编号: CNNVD-201310-455 发布时间: 2013-10-21 ...
- (转载)C++ const成员初始化问题
(转载)http://www.189works.com/article-45135-1.html Const成员如其它任何成员一样,简单考虑其出现在三个位置:全局作用域.普通函数内部.类里面. 下面请 ...
- Excel通过宏创建百万数据
打开视图->宏->编辑,代码如下,cells(n,m)表示当前Excel表格第n行第m列</span> Sub newdata() Dim i As Long Cells(i, ...
- 关于缓存的tips——HTTP权威指南读书心得(十三)
上一章介绍了缓存新鲜度判断的基本原理,本章对于缓存新鲜度判断方法进行一些补充(更新间隔略长略长..). 关于缓存的TIPS 服务器可以通过http定义的几种header对可以缓存数据的存在时间进行控制 ...
- HDU 4714 Tree2cycle DP 2013杭电热身赛 1009
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 Tree2cycle Time Limit: 15000/8000 MS (Java/Other ...
- JSON AND BSON
JSON JavaScript Object Notation (JSON) is an open, human and machine-readable standard that facilita ...
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- hdoj 2401 Baskets of Gold Coins
Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 手机APP上下滚动翻页效果
//页面初期加载时 $(document).ready(function () { //加载第一页 LoadList(); //滚动换页 $( ...