java中io流下载文件实例
来源:互联网 发布:阿里云短信 regionid 编辑:程序博客网 时间:2024/05/20 05:53
public static void saveUrlAs(String Url, File fileName){
//此方法只能用HTTP协议
//保存文件到本地
//Url是文件下载地址,fileName 为一个全名(路径+文件名)文件
URL url;
DataOutputStream out = null;
DataInputStream in = null;
try {
url = new URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
in = new DataInputStream(connection.getInputStream());
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);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//此方法只能用HTTP协议
//保存文件到本地
//Url是文件下载地址,fileName 为一个全名(路径+文件名)文件
URL url;
DataOutputStream out = null;
DataInputStream in = null;
try {
url = new URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
in = new DataInputStream(connection.getInputStream());
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);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- java中io流下载文件实例
- java中io流下载文件实例
- Java文件流下载文件
- IO流下载文件,支持中文
- 通过流下载文件
- struts2 IO流下载文件及上传文件
- webApi——通过文件流下载文件的实例
- java中使用输出流下载文件弹出打开保存框
- java中使用输出流下载文件弹出打开保存框
- hessian协议从rpc服务获取inputStream流下载文件的方式,出现异常 java.io.IOException: stream is closed
- java:文件流下载文档
- C#通过文件流下载文件
- ASP.NET(C#) Web Api通过文件流下载文件到本地实例
- 使用文件流下载文件功能实现 [原]
- 黑马程序员——Java中IO流下
- IO流下
- java爬取下载文件
- java web中下载文件的问题
- Makefile内置变量,递归式变量,直接展开式变量,条件赋值,追加赋值,Makefile中shell命令的执行
- 世界十大最漂亮钞票
- Android UI开发第十九篇——介绍一个开源的gifview——Android显示GIF动画
- JVM参数
- const_cast示例
- java中io流下载文件实例
- 使用ld的wrap选项替换已有库函数
- CentOS 安装中文输入法
- JavaScript实现页面跳转的几种方式
- memory hotplug
- Opencv中的cvWaitkey函数的用法
- Java JList应用
- Android UI开发第二十篇——仿launcher的左右滑动(用ViewPager实现欢迎引导页面)
- 时不时停下脚步,想想···