需要对一个ERP系统中的数据进行批量更新,因为数据量太多,又不能直接操作数据库,所以想通过JAVA编程来实现,现在没有头绪,想请教各位有没有这方面的高招?
2条回答 默认 最新
- qiemengdao 2011-12-23 11:11关注
给你两种方法供选择:
1),利用httpclient4.× 写一个http的客户端,模拟浏览器请求,如下:
[code]
public void post(List payload) throws UmcException,
ClientProtocolException, IOException {
HttpPost post = new HttpPost(uri);
HttpEntity result = null;
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(payload,
charset);
post.setEntity(entity);
if (LOG.isDebugEnabled()) {
LOG.debug("sending:" + payload);
}HttpResponse response = _httpClient.execute(post); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() != HttpStatus.SC_OK) { result = response.getEntity(); StringBuilder msg = new StringBuilder(); msg.append("http response with code " + statusLine.getStatusCode()); msg.append("\n"); msg.append("post request: " + post.getURI()); msg.append("\n"); msg.append(statusLine.getReasonPhrase()); if (result != null) { msg.append("\n\n"); msg.append(EntityUtils.toString(result, "UTF-8")); msg.append("\n\n"); } throw new UmcException(msg.toString()); } if (response.getEntity() != null) { BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { if (line.indexOf("success") < 0) System.out.println(line); } } } finally { if (result != null) try { EntityUtils.consume(result); } catch (IOException e) { } post.abort(); } }
[/code]
uri是请求的地址,charset是编码“UTF-8”,List就是表单参数集
[code]ClientConnectionManager ccManager = new ThreadSafeClientConnManager();
HttpClient _httpClient = new DefaultHttpClient(ccManager);
[/code]
这是从我的项目测试代码中直接copy出来的,你看着改吧。第二种方法:采用JDK的HttpConnection构造http客户端,
[code]
////发送
HttpURLConnection conn = null;
try {
URL url = new URL(ECP_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setUseCaches(false);
conn.setDoOutput(true);
OutputStreamWriter osw = new OutputStreamWriter(
conn.getOutputStream());
StringBuffer sb = new StringBuffer();
addPair(sb, "p1", "p1value");
addPair(sb, "p2", "p2value");
osw.write(sb.substring(0, sb.length() - 1));
osw.flush();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = null;
sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
line = sb.toString();
// 处理返回的字符串line
return;
////
} catch (IOException e) {
// handle e
} finally {
if (conn != null)
conn.disconnect();
}///发送结束
[/code]
addPair方法:
[code]
public static void addPair(StringBuffer sb, String name, String value) {
if (value == null) {
return;
}
sb.append(name);
sb.append("=");
sb.append(value);
sb.append("&");
}
[/code]本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥60 版本过低apk如何修改可以兼容新的安卓系统
- ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?