在上篇文章 如何備份微信朋友圈的視頻照片 中,咱們下載到exported_sns.json
文件以後,用Notepad++作文字處理而後再用一個Powershell程序來作批量下載。java
因爲Notepad++裏面的文字處理有些複雜,我在想能不能用一個Java程序來統一處理呢?shell
嘗試了一下,發現不太複雜,Java果真處理各類事情都比較方便。程序以下:json
注意在RegExp裏面,必須用四個反斜槓\\\\
表明一個反斜槓\
segmentfault
package com.pwc; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WechatMomentDownload { public static String jsonFile = "C:\\Download\\exported_sns_gary.json"; public static String outputFolder = "C:\\Download\\gary_pic\\"; public static void main(String[] args) throws IOException { BufferedReader reader = null; String sLine = ""; String sContent = ""; String sLink = ""; URL url = null; String outputFile = ""; ArrayList<String> aLink = new ArrayList<String>(); String Regex1 = "CDATA\\[http\\:\\\\\\/\\\\\\/(sh)?mmsns([^]]*)\\/0\\]"; reader = new BufferedReader( new InputStreamReader(new FileInputStream(new File(jsonFile)), "UTF-8")); if (reader != null) { while ((sLine = reader.readLine()) != null) { sContent = sContent + sLine; } reader.close(); } Pattern pattern = Pattern.compile(Regex1); Matcher matcher = pattern.matcher(sContent); int count = 0; System.out.println("Start processing..."); while(matcher.find()) { count++; sLink = sContent.substring(matcher.start() + 6, matcher.end() - 1); sLink = sLink.replaceAll("\\\\/", "/"); aLink.add(sLink); } System.out.println(count + " pictures were found"); for (String sLinkTemp : aLink) { url = new URL(sLinkTemp); DataInputStream dataInputStream = new DataInputStream(url.openStream()); outputFile = outputFolder + count + ".jpg"; FileOutputStream fileOutputStream = new FileOutputStream(new File(outputFile)); ByteArrayOutputStream output = new ByteArrayOutputStream(); System.out.println("Downloading " + sLinkTemp + " to file " + outputFile); byte[] buffer = new byte[1024]; int length; while ((length = dataInputStream.read(buffer)) > 0) { output.write(buffer, 0, length); } fileOutputStream.write(output.toByteArray()); dataInputStream.close(); fileOutputStream.close(); count--; } System.out.println("End of processing..."); } }