下载远程应用服务器下的文件

HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(
"http://127.0.0.1:8089/test/test.doc");
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("获取失败: " + getMethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getMethod.getResponseBody();

String serverfile = "d:/test.doc";
OutputStream serverout = new FileOutputStream(serverfile);

serverout.write(responseBody);
serverout.flush();
serverout.close();
System.out.println("下载成功!");
this.setResult("下载成功!");

} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}

return null;

你可能感兴趣的:(应用服务器,网络应用,网络协议)