网站地图
  
  高级搜索
  首页   技术论坛   博客 派计划   产品中心   资源中心   银弹在线   商城  





两种用http协议获取文件的方法    
#1楼
给作者发送短消息 给作者发送短消息 实名会员 
查看用户其他信息
总分 12 分
财富 97 goCom币
威望 21
排名 :(
段位 新手必读

      在项目中用到用http协议获取文件的方法,试了一下 以下两种方法均可,写下来供大家参考

 方法一

    需要用到commons-httpclient-3.0.jar

 import="org.apache.commons.httpclient.*";

 import="org.apache.commons.httpclient.methods.*;

HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

GetMethod httpMethod = new GetMethod(XMLFile);

httpClient.executeMethod(httpMethod);

if (httpMethod.getStatusCode() == HttpStatus.SC_OK)

{

String str = httpMethod.getResponseBodyAsString(); out.println(str );

 }

方法二

  import="java.net.URL";

import="java.net.HttpURLConnection";

 URL url = new URL(XMLFile);

urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setDoOutput(true);

urlConnection.setDoInput(true);

urlConnection.setUseCaches(false);

 InputStream in = urlConnection.getInputStream();

 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(in));

StringBuffer temp = new StringBuffer();

String line = bufferedReader.readLine();

while (line != null) {

temp.append(line).append("\n");

line = bufferedReader.readLine();

}

bufferedReader.close();

xmlString = new String(temp.toString().getBytes(), "UTF-8" );

 

 




发表回复
账号用户名   密码   登录
内容:url email imgsrc image code quote
范例 Example
bold italic underline linethrough   


 [更多...]