GetImage
check路径是是否有效,网络访问地址。notfound查找不到,httpstatuscode枚举数较多,根据自己想要的设置就好了。
public bool checkValid(string path)
{
HttpWebRequest request = HttpWebRequest.Create(path) as HttpWebRequest;
request.Method = "GET";
request.ProtocolVersion = new Version(1, 1);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode==HttpStatusCode.NotFound)
{
return false;
}
return true;
}
根据网络地址还是本地路径,进行获取图片数据,保存到本地文件下。注意数据流的权限FileAccess.ReadWrite
保存有可能出错,在外层套了个try catch,不过各个节进行判断更好,能够拿到出错点。
if (url.IndexOf("http://") == 0 || url.IndexOf("https://") == 0)
{
WebRequest request = (WebRequest)HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
FileStream fileStream = File.Create(filePath);
byte[] buffer = new byte[(int)response.ContentLength];
int numReadByte = 0;
while ((numReadByte = stream.Read(buffer, 0, (int)response.ContentLength)) != 0)
{
fileStream.Write(buffer, 0, numReadByte);
}
fileStream.Close();
stream.Close();
}
else
{
FileStream fs = File.Open(url, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
FileStream fileStream = File.Create(filePath);
Byte[] image = new Byte[(int)fs.Length];
int numReadByte = 0;
while ((numReadByte = fs.Read(image, 0, (int)fs.Length)) != 0)
{
fileStream.Write(image, 0, numReadByte);
}
fs.Close();
fileStream.Close();
}
GetImage的更多相关文章
- Toolkit.getImage获取图片
public class Img { private static final Toolkit tk = Toolkit.getDefaultToolkit(); public static fina ...
- Browser GetImage
using Microsoft.Win32; using System; using System.ComponentModel; using System.Drawing; using System ...
- js调用echarts getImage方法 将图表转换为img
function chart(opt,id,chartName){//配置option的方法 var chartName = echarts.init(document.getElementById( ...
- Clipboard.GetImage() Clipboard获取粘贴板内容为null的解决办法
将线程启动模式设置为 sta 单线程 简介 STA: Single-Thread Apartment, 中文叫单线程套间.就是在COM库初始化的时候创建一个内存结构,然后让它和调用CoIn ...
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- 开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)
在这个.NET组件的介绍系列中,受到了很多园友的支持,一些园友(如:数据之巅. [秦时明月]等等这些大神 )也给我提出了对应的建议,我正在努力去改正,有不足之处还望大家多多包涵.在传播一些简单的知识的 ...
- JBPM
JBPM简介 什么是jbpm JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩 ...
- mono for android 读取网络远程图片
布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=& ...
- c#文件操作
1.创建文件夹 //using System.IO; Directory.CreateDirectory(%%1); 2.创建文件 //using System.IO; File.Create(% ...
随机推荐
- erlang自定义数据类型
Erlang系统自带的基础数据类型有:atom.tuple.list.binary.pid.float.number.port.reference.record. 用户可以通过通过命令type来自定义 ...
- IPv4&IPv6双重协议栈
IPV4 TCP客户与IPV6服务器之间的通信: 1 启动IPV6服务器,创建套接监听口,绑定通配地址 2 IPV4调用gethostbyname找到该服务器对应的A记录 3 调用connect,向服 ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- 编辑器TP
http://www.itshipin.com/blog/archives/category/php/thinkphp
- Understanding page frames and pages
Memory in Linux is organized in the form of pages (typically 4 KB in size). Contiguous linear addres ...
- oc-25- @property @synthesize
s.h #import <Foundation/Foundation.h> @interface Student : NSObject { @public NSString *_name; ...
- IPC——流套接字通信
Linux进程间通信——使用流套接字 前面说到的进程间的通信,所通信的进程都是在同一台计算机上的,而使用socket进行通信的进程可以是同一台计算机的进程,也是可以是通过网络连接起来的不同计算机上的进 ...
- gpus_ReturnGuiltyForHardwareRestart 错误
经查出是glScissor长宽不能为0,在某些设备上会出问题
- 用户环境配置文件/etc/profile
当用户在登录界面正确地输入用户名和密码后,系统就开始为用户构建一个可以使用的用户环境.用户环境包括用户使用的环境变量.快捷键设置及命令别名等.这些设置大多是通过运行全局用户配置文件/etc/profi ...
- RedHat Linux 安装oracle11g
1.准备oracle安装文件Oracle11gR2包含两个文件linux_11gR2_database_1of2.zip和linux_11gR2_database_2of2.zip,将这两个文件通过S ...