新浪博客照片导出xml,使用java导出
Posted lansaviour
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了新浪博客照片导出xml,使用java导出相关的知识,希望对你有一定的参考价值。
最近家人碰到这种需求,说导出来是xml文件,还需要一步步的点开自己保存,既然有地址,那写个简单的程序还是更方便一些,这里面没有使用多线程,2000多张照片也挺快的;
直接看代码:
1 import java.io.ByteArrayOutputStream; 2 import java.io.DataInputStream; 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 import java.util.Map.Entry; 13 14 import org.dom4j.Document; 15 import org.dom4j.Element; 16 17 public class ExportXLPhoto 18 private static String xmlPath = "H:/xinlangphotos.xml"; //你的xml文件地址 19 public static void main(String[] args) 20 int i =1; 21 Map<String, List<String>> urlMap = getSqls(); 22 String path="H:/XLphotos/"; //你需要保存本地的目录 23 for(Entry<String, List<String>> entry : urlMap.entrySet()) 24 String fileName = entry.getKey(); 25 List<String> urList = entry.getValue(); 26 for (String url : urList) 27 downloadPicture(url,path+fileName,path+fileName+"/"+i+".jpg"); 28 i++; 29 30 31 32 33 34 public static Map<String,List<String>> getSqls() 35 36 Map<String, List<String>> urlMap = new HashMap<>(); 37 38 Document document = XmlUtil.readXML(xmlPath); 39 Element root = document.getRootElement(); 40 List<Element> els = root.elements(); 41 for (Element el : els) 42 List<String> urls = new ArrayList<String>(); 43 System.out.println("目录:"+el.getName()); 44 45 List<Element> elementList=el.elements(); 46 for (Element element : elementList) 47 urls.add(element.getText()); 48 System.out.println("节点:"+element.getName()); 49 50 urlMap.put(el.getName(), urls); 51 52 return urlMap; 53 54 55 56 private static void downloadPicture(String urlList,String fileLocation,String path) 57 URL url = null; 58 try 59 url = new URL(urlList); 60 61 File file =new File(fileLocation); 62 if (!file .exists() && !file .isDirectory()) 63 64 // System.out.println("//不存在"); 65 file .mkdir(); 66 67 68 69 DataInputStream dataInputStream = new DataInputStream(url.openStream()); 70 71 FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); 72 ByteArrayOutputStream output = new ByteArrayOutputStream(); 73 74 byte[] buffer = new byte[1024]; 75 int length; 76 77 while ((length = dataInputStream.read(buffer)) > 0) 78 output.write(buffer, 0, length); 79 80 fileOutputStream.write(output.toByteArray()); 81 dataInputStream.close(); 82 fileOutputStream.close(); 83 catch (MalformedURLException e) 84 e.printStackTrace(); 85 catch (IOException e) 86 e.printStackTrace(); 87 88 89
需要读取xml文件,所以有xmlUtil:
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.InputStream; 4 5 import org.dom4j.Document; 6 import org.dom4j.DocumentException; 7 import org.dom4j.io.SAXReader; 8 9 10 public class XmlUtil 11 public static Document readXML(String path) 12 SAXReader reader = new SAXReader(); 13 Document document = null; 14 try 15 InputStream inputStream = new FileInputStream(path);//加载文件 16 document = reader.read(inputStream); 17 catch (DocumentException e) 18 e.printStackTrace(); 19 catch (FileNotFoundException e) 21 e.printStackTrace(); 22 23 return document; 24 25 26
然后 就执行main函数就ok了。
因为着急导出,就简单的写了写,也没有做出什么优化,还请多指教吧!
以上是关于新浪博客照片导出xml,使用java导出的主要内容,如果未能解决你的问题,请参考以下文章