服务器端:

1.新建一个Asp.net空网站RGImageServer。

2.新建一个WebService项目ImageService,项目新增文件ImageService.asmx,添加方法GetTile()。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Configuration; namespace RGImageServer
{
/// <summary>
/// ImageServices 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class ImageServices : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
private byte[] Stream2Bytes(Stream theStream)
{
int num;
MemoryStream stream = new MemoryStream();
while ((num = theStream.ReadByte()) != -1)
{
stream.WriteByte((byte)num);
}
theStream.Close();
return stream.ToArray();
}
[WebMethod]
public void GetTile(string imageName)
{
string filename = ConfigurationManager.AppSettings["ImagePath"] + @"\" + imageName;
HttpContext context = this.Context;
if (File.Exists(filename))
{
try
{
FileStream theStream = File.OpenRead(filename);
context.Response.ContentType = "image/png";
byte[] buffer = Stream2Bytes(theStream);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
catch (Exception)
{
context.Response.StatusCode = 500;
}
}
}
}
}

3.配置WebConfig文件,发布服务。

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
<add name="HttpSoap" />
</protocols>
</webServices>
</system.web>
<appSettings>
<add key="ImagePath" value="E:\"/>
</appSettings>
</configuration>

客户端:

1.调用下载图片代码如下:

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stream ContentStream;
HttpWebResponse response = null;
string ContentType;
string ContentEncoding;
int ContentLength = ;
int BytesProcessed;
private void button1_Click(object sender, EventArgs e)
{
ImageServices server = new ImageServices();
string Url = "http://localhost:6235/ImageServices.asmx/GetTile?imageName=aa.png";
string SavedFilePath = "D:\\bb.png";
// Download to file
string targetDirectory = Path.GetDirectoryName(SavedFilePath);
if (targetDirectory.Length > )
Directory.CreateDirectory(targetDirectory);
ContentStream = new FileStream(SavedFilePath, FileMode.Create);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.ContentType = "text/xml";
using (response = request.GetResponse() as HttpWebResponse)
{
// only if server responds 200 OK
if (response.StatusCode == HttpStatusCode.OK)
{
ContentType = response.ContentType;
ContentEncoding = response.ContentEncoding; // Find the data size from the headers.
string strContentLength = response.Headers["Content-Length"];
if (strContentLength != null)
{
ContentLength = int.Parse(strContentLength);
}
//缓存字节数组,大小1500byte
byte[] readBuffer = new byte[];
using (Stream responseStream = response.GetResponseStream())
{
while (true)
{
// Pass do.readBuffer to BeginRead.
int bytesRead = responseStream.Read(readBuffer, , readBuffer.Length);
if (bytesRead <= )
break;
ContentStream.Write(readBuffer, , bytesRead);
BytesProcessed += bytesRead;
}
}
ContentStream.Close();
ContentStream.Dispose();
ContentStream = null;
}
} }
}

以下是一个一般处理程序ImageHandler用于图片发布,添加ImageHandler.ashx:

 using System;
using System.Web;
using System.Configuration;
using System.IO; namespace RGImageServer
{
/// <summary>
/// ImageHandler 的摘要说明
/// </summary>
public class ImageHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World"); string imageName = "aa.png";
string filename = ConfigurationManager.AppSettings["ImagePath"] + @"\" + imageName;
if (File.Exists(filename))
{
try
{
FileStream theStream = File.OpenRead(filename);
context.Response.ContentType = "image/png";
byte[] buffer = StreamToBytes(theStream);
context.Response.OutputStream.Write(buffer, , buffer.Length);
}
catch (Exception)
{ }
}
}
private byte[] StreamToBytes(Stream theStream)
{
int num;
MemoryStream stream = new MemoryStream();
while ((num = theStream.ReadByte()) != -)
{
stream.WriteByte((byte)num);
}
theStream.Close();
return stream.ToArray();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

ImageHandler

通过浏览器可以直接访问:http://localhost:6235/ImageHandler.ashx

利用WebService发布图片文件的更多相关文章

  1. 利用Selenium实现图片文件上传的两种方式介绍

    在实现UI自动化测试过程中,有一类需求是实现图片上传,这种需求根据开发的实现方式,UI的实现方式也会不同. 一.直接利用Selenium实现 这种方式是最简单的一种实现方式,但是依赖于开发的实现. 当 ...

  2. C#利用WebService接口下载文件

    WebTest.RtTfSimDataInterface test = new WebTest.RtTfSimDataInterface(); //string strBasic = test.Get ...

  3. 利用VS2008发布一个简单的webservice

    一个开发好的webservice,怎样发布出去,供其他电脑访问呢? 本文将介绍如何发布一个简单的webservice,其中的内容都是在网上查看别人文章,自己仿照着做了一遍,因此,难免会发生错误,如果发 ...

  4. winform利用ImageList控件和ListView控件组合制作图片文件浏览器

    winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...

  5. winform利用itextsharp.dll实现图片文件转换PDF格式文件

    1.利用itextsharp.dll实现单个图片文件转换为PDF格式文件, 可以使用以下类: void ConvertJPG2PDF(string jpgfile, string pdf) { var ...

  6. 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片

    1.读取图片数据 函数原型:bool LoadImage(const char *pName, unsigned char *pBitData); 函数功能,读取pName指向的图片文件的位图数据 b ...

  7. 利用gulp把本地文件移动到指定待发布文件夹

    一.目标 把本地的文件移动到待发布的文件中,把static_grab文件中file.txt所列文件列表移动到beta对应文件夹中: 二.实现 var gulp = require('gulp'), w ...

  8. Java利用Base64编码和解码图片文件

    1.编码与解码代码如下所示: import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import jav ...

  9. ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件

    虽然ASP.NET Core是一款"动态"的Web服务端框架,但是在很多情况下都需要处理针对静态文件的请求,最为常见的就是这对JavaScript脚本文件.CSS样式文件和图片文件 ...

随机推荐

  1. JSP简单访问数据库

    Java代码 public class DBHelper { private String driverName; private String url; private String user; p ...

  2. Web 在线文件管理器学习笔记与总结(8)删除文件

    unlink($filename) 删除文件 index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...

  3. [收藏] 关于解决“进程com.android.phone意外停止”的方法 (未尝试)

    很多机油反应有这个情况,本人费劲九牛20虎之力终于克服之,这个现象一般出现在刚刷完系统会出现,甚至你怎么刷ROM这个现象依旧存在(崩溃不?)~~~有位机油刷了这个系统也出现了http://samsun ...

  4. Ubuntu+Nginx+PHP的最简搭建方法

    先安装: sudo apt-get install nginx php5-fpm -y 然后编辑配置文件: /etc/nginx/site-available/default 找到"loca ...

  5. 20145235 《Java程序设计》第6周学习总结

    教材学习内容总结 10.1 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度来看,如果要将 ...

  6. web_custom_request函数详解

    在LR中当使用HTML录制方式时,录制的脚本中主要由函数web_link().web_submit_form().web_url().web_submit_data()组成,当使用HTTP录制方式时, ...

  7. 11号了,还有三天上线-改bug

    +(NSDictionary *)replacedKeyFromPropertyName { return  @{ @"doctorId": @"id" }; ...

  8. changing a pointer rather than erasing memory cells

    Computer Science An Overview _J. Glenn Brookshear _11th Edition In a data structure based on a point ...

  9. php源码安装

    要用swoole,首先需要有PHP环境.由于swoole的某些特性,最好是能够从源码编译安装PHP,这样在使用过程中可以避免很多不必要的错误.PHP下载地址:http://php.net/在这里挑选你 ...

  10. 简单CMakeLists.txt文件

    #CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(server) #添加包含目录 include_directories(./in ...