使用C#下载网络文件
下载
/// <summary>
/// 下载文件
/// </summary>
/// <param name="URL">下载文件地址</param>
/// <param name="Filename">下载后另存为(全路径)</param>
private bool DownloadFile(string URL, string filename)
{
try
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse) Myrq.GetResponse();
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
byte[] by = new byte[];
int osize = st.Read(by, , (int) by.Length);
while (osize > )
{
so.Write(by, , osize);
osize = st.Read(by, , (int) by.Length);
}
so.Close();
st.Close();
myrp.Close();
Myrq.Abort();
return true;
}
catch (System.Exception e)
{
return false;
}
}
解压Zip文件
下面代码主要用到了SharpZipLib的库
private string UnZip(string entryname, ZipFile zf)
{
var cover = zf.GetEntry(entryname);
if (cover == null)
{
return null;
}
string filepath = System.AppDomain.CurrentDomain.BaseDirectory + @"pic\";
string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + RY.Common.DirectoryAndFile.GetFileExt(entryname); string name = filepath + filename;
using (FileStream writer = System.IO.File.Create(name)) //解压后的文件
{
var stream = zf.GetInputStream(cover);
int bufferSize = * ; //缓冲区大小
int readCount = ; //读入缓冲区的实际字节
byte[] buffer = new byte[bufferSize];
readCount = stream.Read(buffer, , bufferSize);
while (readCount > )
{
writer.Write(buffer, , readCount);
readCount = stream.Read(buffer, , bufferSize);
}
writer.Close();
}
return "pic/" + filename;
}
使用C#下载网络文件的更多相关文章
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- Java读取并下载网络文件
CreateTime--2017年8月21日10:11:07 Author:Marydon import java.io.ByteArrayOutputStream; import java.io ...
- python下载网络文件
python下载网络文件 制作人:全心全意 下载图片 #!/usr/bin/python #-*- coding: utf-8 -*- import requests url = "http ...
- DELPHI TDownLoadURL下载网络文件
DELPHI XE6 FMX 附件:http://files.cnblogs.com/xe2011/IDHttp_fmx.7z unit Unit1; interface uses //引用 Vc ...
- 【python】下载网络文件到本地
# 下载网络图片文件到本地 import urllib.request rsp=urllib.request.urlopen("http://n.sinaimg.cn/ent/transfo ...
- java 下载网络文件
1.FileUtils.copyURLToFile实现: import java.io.File; import java.net.URL; import org.apache.commons.io. ...
- python使用wget下载网络文件
wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. ubuntu 安装wget pip install wget 从网络或本地硬盘下载文件(并 ...
- 解决FTPClient下载网络文件线程挂起问题
今天在windows上调试FTP下载文件时,出险线程假死,代码如下: if (inputStream != null) { byte[] data = null; ByteArrayOutputStr ...
- C#限速下载网络文件
代码: using System; using System.Collections.Concurrent; using System.Collections.Generic; using Syste ...
- 网络编程(一):用C#下载网络文件的2种方法
使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用 ...
随机推荐
- NS3 日志(Logging)、命令行参数、Tracing系统概述(转载)
NS-3日志子系统的提供了各种查看仿真结果的渠道: 一.使用Logging Module 1 [预备知识]日志级别及其对应的宏 NS-3 提供了若干个日志级别来满足不同的 Debug 需求,每一级的日 ...
- oracle的基本查询~下
SQL> --别名SQL> select ename 姓名, job as "工作" ,sal "薪水" from emp; 姓名 ...
- 2013 Multi-University Training Contest 9
HDU-4687 Boke and Tsukkomi 题意:给定一个简单图,询问哪些边如果选择的话会使得最大的连边数减少. 解法:套用一般图的最大匹配算法(带花树)先算出最大匹配数,然后枚举一条边被选 ...
- iOS - UIScrollView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> @available(iOS 2.0, ...
- Spring字符集过滤器CharacterEncodingFilter
Spring中的字符集过滤器可以很方便的为我们解决项目中出现的中文乱码问题,而且使用方法也很简单,只需要在web.xml文件中配置一下该过滤器,设置两个重要的参数(encoding和forceEnco ...
- bootstrap学习笔记<五>(表单一)
表单 bootstrap为表单提供三种样式:默认表单,水平表单,内联表单. <form class="form-horizontal" role="form&quo ...
- lmdb存储的一些库相关函数
MDB_env 为一个结构体,Opaque structure for a database environment. MDB_txn :Opaque structure for a transact ...
- 使用Ant打包工具 基本介绍
由于使用java,javac,jar等工具进行编译打包,即繁琐低效又容易出错,因此Ant出现了. Ant的出现就是专门为了打包编译java代码的,使用之前得稍微学一下.Ant的运行起来主要是依靠配置文 ...
- Spring源码分析心得-Bean的读取和注册
Spring广泛的被使用足以证明它的价值,成功的东西自然值得我们深入学习.有很多人说的熟悉Spring也只是简单的使用经验,当我发现我也是其中一员时我很恐惧,所以开始扒源码,准备深入了解,话不多说,直 ...
- form属性method="get/post
form属性method="get/post"的两种方式对比 在B/S应用程序中,前台与后台的数据交互,都是通过HTML中Form表单完成的.Form提供了两种数据传输的方式——g ...