JAVA读取网络文件 转
2011-05-27 20:29
441 查看
JAVA读取网络文件 转
标签:
1、HTTP方式:
/**
* 通过HTTP方式获取文件
*
* @param strUrl
* @param fileName
* @return
* @throws IOException
*/
private boolean getRemoteFile(String strUrl, String fileName) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
DataInputStream input = new DataInputStream(conn.getInputStream());
DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[1024 * 8];
int count = 0;
while ((count = input.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
output.close();
input.close();
return true;
}
调用时使用下面的数据测试通过,本地得到了test.gif:
String fileUrl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
String fileName = "test.gif";
支持FTP方式的获取,只需要如下改动:
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
URLConnection conn = url.openConnection();
下面的测试代码也成功执行,本地环境获得了cu_html.zip文件:
public static void main(String[] args) throws IOException {
String fileUrl = "ftp://ftp.cuhk.hk/pub/cu_html.zip";
String fileName = "cu_html.zip";
Test1 test = new Test1();
System.out.println(test.getRemoteFile(fileUrl, fileName));
}
标签:
1、HTTP方式:
/**
* 通过HTTP方式获取文件
*
* @param strUrl
* @param fileName
* @return
* @throws IOException
*/
private boolean getRemoteFile(String strUrl, String fileName) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
DataInputStream input = new DataInputStream(conn.getInputStream());
DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[1024 * 8];
int count = 0;
while ((count = input.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
output.close();
input.close();
return true;
}
调用时使用下面的数据测试通过,本地得到了test.gif:
String fileUrl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
String fileName = "test.gif";
支持FTP方式的获取,只需要如下改动:
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
URLConnection conn = url.openConnection();
下面的测试代码也成功执行,本地环境获得了cu_html.zip文件:
public static void main(String[] args) throws IOException {
String fileUrl = "ftp://ftp.cuhk.hk/pub/cu_html.zip";
String fileName = "cu_html.zip";
Test1 test = new Test1();
System.out.println(test.getRemoteFile(fileUrl, fileName));
}
相关文章推荐
- Java读取并下载网络文件
- java读取网络文件
- java网络编程—读取html存储到文件中
- 使用java读取url网络文件
- 关于java读取远程文件时网络超时判断
- java对.txt文件进行读取方法实战---室友是网络安全方向,帮他提取文件里的特定ip
- Java读取并下载网络文件的方法
- Java读取网络文件
- java读取网络资源文件(用于请求短信发送功能)
- JAVA读取网络文件
- Java读取property配置文件的方法
- java使用java.util.Properties读取properties文件的九种方法
- Java中,当表单含有文件上传时,提交数据的如何读取
- java14.语言进阶------本地文件操作---文件属性的读取、设置
- java读取TXT文件的方法
- java如何读取和遍历properties文件
- [Java] 读取文件
- Java利用SMB上传、下载、读取(内容或文件名)、复制、删除远程文件
- java读取文件为string
- java读取.properties配置文件