新浪博客照片导出xml,使用java导出

本文介绍了一个简单的Java程序,用于从XML文件中批量下载照片到指定目录。程序通过解析XML文件获取图片URL,并逐个下载保存。

最近家人碰到这种需求,说导出来是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了。

 

因为着急导出,就简单的写了写,也没有做出什么优化,还请多指教吧!

转载于:https://www.cnblogs.com/lansaviour/p/11193111.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值