从URL读取文件保存到本地
来源:互联网 发布:单片机节日彩灯设计 编辑:程序博客网 时间:2024/04/28 13:09
<%@page import="java.net.*,java.io.*"%>
<%!
public boolean saveUrlAs(String photoUrl, String fileName) {
//此方法只能用户HTTP协议
try {
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
return true;
}
catch (Exception e) {
return false;
}
}
public String getDocumentAt(String urlString) {
//此方法兼容HTTP和FTP协议
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.
getInputStream()));
String line = null;
while ( (line = reader.readLine()) != null) {
document.append(line + "/n");
}
reader.close();
}
catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
}
catch (IOException e) {
System.out.println("IOException when connecting to URL: " + urlString);
}
return document.toString();
}
%>
<%
//测试
String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "d:/ghost/";
boolean flag = saveUrlAs(photoUrl, filePath + fileName);
out.println("Run ok!/n<BR>Get URL file " + flag);
%>
<%!
public boolean saveUrlAs(String photoUrl, String fileName) {
//此方法只能用户HTTP协议
try {
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
return true;
}
catch (Exception e) {
return false;
}
}
public String getDocumentAt(String urlString) {
//此方法兼容HTTP和FTP协议
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.
getInputStream()));
String line = null;
while ( (line = reader.readLine()) != null) {
document.append(line + "/n");
}
reader.close();
}
catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
}
catch (IOException e) {
System.out.println("IOException when connecting to URL: " + urlString);
}
return document.toString();
}
%>
<%
//测试
String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "d:/ghost/";
boolean flag = saveUrlAs(photoUrl, filePath + fileName);
out.println("Run ok!/n<BR>Get URL file " + flag);
%>
- 从URL读取文件保存到本地
- 从URL获取文件保存到本地的代码
- 从URL获取文件保存到本地的代码
- 从URL获取文件保存到本地的代码 [轉貼]
- 从URL获取文件保存到本地的代码
- 从URL获取文件保存到本地的代码
- 从URL获取文件保存到本地的代码
- 从URL获取文件保存到本地的代码
- 从URL获取文件保存到本地的JAVA代码
- 从URL获取文件保存到本地的JAVA代码
- url图片 保存文件到本地
- 用URL类读取网络页面源文件保存到本地
- 嵌入式 如何使用jlink从flash中读取数据保存为bin文件到本地
- 从FTP服务器读取文件到本地
- [Android]从视频流读取帧图像保存到本地
- 将URL上的文件保存到本地。
- 将url文件链接保存到本地服务器
- 保存文件到本地
- 创建DLL动态连接库
- 部分常用数据操作代码(一)
- 關於我的計算機理論知識
- 比较SQL Server 2000 数据库中两个库的差异
- 你的定位,你的一生--解读IT人员的从业方向
- 从URL读取文件保存到本地
- ASP.NET ViewState 初探
- QT鼠标点击响应事件
- 网页乱码问题ASP.NET
- 三层架构各层关系分析比较及其约定
- 我的蓝色之路有多远?
- 零碎的记录
- 关于进行Asp.net窗体验证的过程说明
- 导出带有模板的repeator