Java下载-2

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class doload {
	
	public static void main(String[] args) throws IOException {
	
		/*下载TS群*/
		downloadTS(65,896,"23ab34da2b",0,400,"-","v3","video");
		
		/* 合成TS文件*/
		//synthesisTemp("H:\\f40bbcaa","H:\\","f40bbcaa");
	}
	
	/**
	 * 合成TS文件,/b是必需参数
	 * @param souPath 源路径
	 * @param tarPath 目标路径
	 * @param newFileName 合成文件名
	 */
	public static void synthesisTemp(String souPath,String tarPath,String newFileName){
//		if(newFileName.equals("")){ //如果为空,则默认获取文件名文文件夹名
//			
//		}
		execCMD("copy /b "+souPath+"\\*.ts "+tarPath+newFileName+".ts");
		System.out.println("copy /b "+souPath+"\\*.ts "+tarPath+newFileName+".ts");
		System.out.println("合成完成!");
	}
	
	/**
	 * 应该可以支持断点下载
	 * @param indexMain 下载内容的主索引
	 * @param indexSub 下载内容的次索引
	 * @param folder 下载的文件名
	 * @param number 下载内容的开始,为0则为完整下载,不为0则为断点下载
	 * @param indexMax 下载内容的最大索引
	 * @throws IOException
	 * @ejb.transaction
	 *   type=--exec 在运行结束之后运行该命令 ; dl 调用youtube-dl
	 */
	public static void downloadTS(int indexMain,int indexSub,String folder,int number,int indexMax, String kinleStr,String version,String type) throws IOException{
		/**
		 * kinleStr .,-
		 * version v2,v3
		 * type video,ts
		 */
		
		createFolder(folder); //创建文件夹

		//判断文件是否属于断点下载
		int n = 0;
		if(number!=0){
			n = number;
		}
		
		//下载执行体
		for(;n<indexMax+1;n++){
			System.out.println("正在执行(已执行"+n+"次),已完成  "+((float)n/indexMax*100)+"%");
			
			if(kinleStr.equals(".")){
				if(n==0 || n==number){System.out.println("以\".\"为规则,执行下载体");}
				execCMD("dl "
						+ "--exec \"move {} H:\\"
						+folder+"\\{}\" http://" + version + ".*****.com/" + type + "/"
						+indexMain+"/"
						+indexSub+"/"
						+folder+".m3u8."
						+n+".ts");
			}
	
			if(kinleStr.equals("-")){
				String tempZeroNumber = ""; //初始化 临时变量体零数
				if(n==0 || n==number){System.out.println("以\"-\"为规则,执行下载体.(" + (n==0 || n==number) + ")");}
				if(n>999){
					tempZeroNumber="0"+n;
				}else if(n>99){
					tempZeroNumber="00"+n;
				}else if(n>9){
					tempZeroNumber="000"+n;
				}else if(n>-1){
					tempZeroNumber="0000"+n;
				}else{
					System.out.println("系统异常!");
				}
				
				
				execCMD("dl "
						+ "--exec \"move {} H:\\"
						+folder+"\\{}\" http://" + version + ".******.com/" + type + "/"
						+indexMain+"/"
						+indexSub+"/"
						+folder+kinleStr
						+tempZeroNumber+".ts");
			}
		}
		
		//下载结束提示语
		System.out.println("下载结束! ");
	}
	
	/**
	 * 创建文件夹测试成功
	 * @param folder
	 * @throws IOException
	 */
	public static void createFolder(String folder) throws IOException{
		File file = new File("H:\\"+folder);
		if(!file.exists()){
			file.mkdir();
			System.out.println("文件夹"+folder+"创建成功");
		}
	}
	
	/**
	 * 直接输入CMD命令测试
	 * @param command
	 * @return
	 */
	public static String execCMD(String command) {
        StringBuilder sb =new StringBuilder();
        try {
            Process process=Runtime.getRuntime().exec(command);
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while((line=bufferedReader.readLine())!=null)
            {
                sb.append(line+"\n");
            }
        } catch (Exception e) {
            return e.toString();
        }
        return sb.toString();
    }

	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值