吐槽下用cvs模板导入数据这种做法.太二逼了
private static String upload_url = "http://api.map.baidu.com/geodata/v2/poi/create";//导入地址
public static void addData(Map<String, String> maps)
{
String str = sendGet(url, null);//获取相应的导入数据
Type type = new TypeToken<ArrayList<BrikDomain>>()
{
}.getType();
bdList = new Gson().fromJson(str, type);//解析成list
for (BrikDomain bd : bdList)//循环将列表数据添加到云存储
{
maps.clear();
maps.put("ak", ak);//访问key
maps.put("title", bd.getName());
maps.put("address", bd.getAddr());
maps.put("latitude", bd.getLat());
maps.put("longitude", bd.getLon());
maps.put("coord_type", "3");
maps.put("geotable_id", "39873");
maps.put("code", bd.getCode());//以下为lbs云存储自定义字段
maps.put("localCode", bd.getCode());
maps.put("num", bd.getNum());
maps.put("quyu", bd.getQuyu());
HttpUtil.httpPost(upload_url, maps);//提交数据
}
}
public static String sendGet(String url, String param)
{
String result = "";
BufferedReader in = null;
try
{
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet())
{
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
// System.out.println(line);
result += line;
}
System.out.println(result);
} catch (Exception e)
{
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally
{
try
{
if (in != null)
{
in.close();
}
} catch (Exception e2)
{
e2.printStackTrace();
}
}
return result;
}
static class HttpUtil
{
public static String httpPost(String url, Map<String, String> params)
{
URL u = null;
HttpURLConnection con = null;
// 构建请求参数
StringBuffer sb = new StringBuffer();
if (params != null)
{
for (Entry<String, String> e : params.entrySet())
{
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
sb.append("&");
}
sb.substring(0, sb.length() - 1);
}
System.out.println("send_url:" + url);
System.out.println("send_data:" + sb.toString());
// 尝试发送请求
try
{
u = new URL(url);
con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
osw.write(sb.toString());
osw.flush();
osw.close();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
if (con != null)
{
con.disconnect();
}
}
// 读取返回内容
StringBuffer buffer = new StringBuffer();
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String temp;
while ((temp = br.readLine()) != null)
{
buffer.append(temp);
buffer.append("\n");
}
System.out.println(buffer.toString());
} catch (Exception e)
{
e.printStackTrace();
}
return buffer.toString();
}
}