日期:2014-05-16  浏览次数:20706 次

从数据库中读地址,并发http请求得到xml并保存
package com.panpan.datatools;

import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;

import junit.framework.TestCase;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.XMLWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.panpan.util.HttpClientUtils;

public class MsgGenerateXMLhelper extends TestCase {

	@Test
	public void testOp() throws Exception {
		pstmt = con.prepareStatement("select id,url from t_user");
		rs = pstmt.executeQuery();
		LinkedList<Msg> msg_l = new LinkedList<Msg>();
		while (rs.next()) {
			Msg msg = new Msg();
			msg.setId(rs.getInt(1));
			msg.setUrl(rs.getString(2));
			msg_l.add(msg);
		}
		for (Msg msg : msg_l) {
			if(StringUtils.isNotBlank(msg.getUrl())){
				String xml = HttpClientUtils.sendGetRequest(msg.getUrl(), "UTF-8");
				XMLWriter writer = null;
				if (StringUtils.isNotBlank(xml)) {
					try{
						Document doc = DocumentHelper.parseText(xml);
						writer = new XMLWriter(new FileWriter("C:\\xmldata\\"+msg.getId()+".xml"));
						writer.write(doc);
					}catch(Exception ex){
						
					}finally{
						if(writer != null){
							writer.close();
						}
					}
				}
			}
		}
	}

	@Before
	public void setUp() throws Exception {
		String CLASSNAME = "com.mysql.jdbc.Driver";
		String URL = "jdbc:mysql://local:3306/database";
		String UNAME = "root";
		String PWD = "root";
		Class.forName(CLASSNAME);
		con = DriverManager.getConnection(URL, UNAME, PWD);
	}

	@After
	public void tearDown() throws Exception {
		if (rs != null) {
			rs.close();
		}
		if (pstmt != null) {
			pstmt.close();
		}
		if (con != null) {
			con.close();
		}
	}

	Connection con = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	class Msg {
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}
		Integer id;
		String url;
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
	}
}

1楼hongqishi昨天 14:22
InputStream inputStream = HttpClientUtils.sendGetAndReturnStream(msg.getUrl(), "UTF-8");nttttFileOutputStream fos=new FileOutputStream("C:\\xmldata\\"+msg.getId()+".xml"); nttttint data=inputStream.read(); nttttwhile(data!=-1){ ntttt fos.write(data); ntttt data=inputStream.read(); ntttt} nttttfos.close();