Java 下载文件

最近在听小说,很多平台上都是收费的,本着节俭解约的心态到网上找到了免费的,但是使用手机在网页上听小说很是不方便,还不能加速播放。下载后听还是很不错的,但是问题又出来了,几百上千集一个一个下载太慢、太累、太麻烦,所以就写一个批量下载程序。

目录

第一步解析页面中所有下载按钮链接(此链接为页面)

第二步解析下载链接

第三步下载文件并保存

下载测试


//读取 页面内容
public static String readHttp(String urlstr) throws Exception{
		String content = "";
		BufferedReader bReader = null;
		//输入流
		InputStreamReader myReader = null;
		StringBuffer sbuf =new StringBuffer();
		//URL对象
		URL url = new URL(urlstr);
		//输入流
		myReader = new InputStreamReader(url.openStream(),"GBK");
		//把输入流放到装饰器 缓存读取
		bReader = new BufferedReader(myReader);
		String s = null;
		//按行读取网页
		while((s = bReader.readLine()) != null ) {
			sbuf.append(s);
		}
		analysisUrl(sbuf.toString());
		content = sbuf.toString();
		bReader.close();
		myReader.close();

		return content;
	}

第一步解析页面中所有下载按钮链接(此链接为页面)

//解析网页  获取所有按钮链接
public static List<String> analysisUrl(String content){
		List<String> list = new ArrayList<String>();
		String str0 = content.substring(content.indexOf("gaosuxiazai"), content.length());
		
		String str1 = str0.substring(str0.indexOf("<ul>") + 4, str0.indexOf("</ul>"));
//		http://www.ting89.com/
		String urlPath = "http://www.ting89.com/";
		while(str1.indexOf("href") >= 0){
			String str2 = str1.substring(str1.indexOf("href='/") + 7, str1.indexOf("html") + 4);
//			System.out.println(urlPath+str2);
			list.add(urlPath+str2);
			str1 = str1.substring(str1.indexOf(str2) + str2.length(), str1.length());
		}
		
		return list;
	}

第二步解析下载链接

// 解析下载 地址
public static String analysisUrlA(String content){
		String strA = content.substring(content.indexOf("iframe"),content.indexOf("/iframe"));
		String strB = strA.substring(strA.indexOf("url=") + 4,strA.indexOf(".mp3") + 4);
		return strB;
	}

第三步下载文件并保存

/**
 * 
 * @param downloadPath	下载地址
 * @param localPath		本地保存路径
 * @param localName		本地保存文件名称
 */
public static void downloadFile(String downloadPath,String localPath,String localName){
	
	try {
		// 下载
       System.out.println("downloadPath:" + downloadPath);
       //获取URL对象
       URL url = new URL(downloadPath);
       //根据URL打开链接
       URLConnection connection = url.openConnection();
       //从连接处获取输入流对象
       InputStream inputStream = connection.getInputStream();
        
       File file = new File(localPath);
        
       if(!file.exists()){
           file.mkdirs();
       }
       saveFile(localPath + localName, inputStream);
       
	} catch (MalformedURLException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
* 保存
* @param savePath 本地保存路径,包括文件名和后缀名
* @param inStream 输入流
*/
public static void saveFile(String savePath,InputStream inStream){
   System.out.println("savePath:" + savePath);
   
   FileOutputStream fs;
   try {
       fs = new FileOutputStream(savePath);
       byte[] buffer = new byte[1204];
        
       int byteread = 0;
       
       while ((byteread = inStream.read(buffer)) != -1) {
         fs.write(buffer, 0, byteread);
       }
       System.out.println(savePath + "保存成功!");
        
   } catch (Exception e) {
       e.printStackTrace();
   }
}

下载测试

String downloadPath = "http://mp3-2f.ting89.com:9090/2018/30/素女寻仙/001.mp3";
String localPath = "F:/mp31/素女寻仙/";
String localName = "素女寻仙001.mp3";


downloadFile(downloadPath,localPath,localName)

END

 

并不是所有页面都可以解析,但是所有下载地址都可以下载。

如果你也想批量下载小说,第一你要找免费网站免费小说,第二你要重新写地址解析规则。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马志武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值