标签:
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(); } } }
标签:
原文地址:http://www.cnblogs.com/Jsonlu/p/5718897.html