利用WebService发布图片文件
服务器端:
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发布图片文件的更多相关文章
- 利用Selenium实现图片文件上传的两种方式介绍
在实现UI自动化测试过程中,有一类需求是实现图片上传,这种需求根据开发的实现方式,UI的实现方式也会不同. 一.直接利用Selenium实现 这种方式是最简单的一种实现方式,但是依赖于开发的实现. 当 ...
- C#利用WebService接口下载文件
WebTest.RtTfSimDataInterface test = new WebTest.RtTfSimDataInterface(); //string strBasic = test.Get ...
- 利用VS2008发布一个简单的webservice
一个开发好的webservice,怎样发布出去,供其他电脑访问呢? 本文将介绍如何发布一个简单的webservice,其中的内容都是在网上查看别人文章,自己仿照着做了一遍,因此,难免会发生错误,如果发 ...
- winform利用ImageList控件和ListView控件组合制作图片文件浏览器
winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...
- winform利用itextsharp.dll实现图片文件转换PDF格式文件
1.利用itextsharp.dll实现单个图片文件转换为PDF格式文件, 可以使用以下类: void ConvertJPG2PDF(string jpgfile, string pdf) { var ...
- 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片
1.读取图片数据 函数原型:bool LoadImage(const char *pName, unsigned char *pBitData); 函数功能,读取pName指向的图片文件的位图数据 b ...
- 利用gulp把本地文件移动到指定待发布文件夹
一.目标 把本地的文件移动到待发布的文件中,把static_grab文件中file.txt所列文件列表移动到beta对应文件夹中: 二.实现 var gulp = require('gulp'), w ...
- Java利用Base64编码和解码图片文件
1.编码与解码代码如下所示: import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import jav ...
- ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件
虽然ASP.NET Core是一款"动态"的Web服务端框架,但是在很多情况下都需要处理针对静态文件的请求,最为常见的就是这对JavaScript脚本文件.CSS样式文件和图片文件 ...
随机推荐
- Sql Server 2008:调试
调试T-SQL语句: 1.Debug普通T-SQL语句: SQL代码如下: use northwind go declare @i int ,@j int,@k int set @i = 1; set ...
- Swing 刷新容器
JPanel pchks = new JPanel();// 容器刷新(重新layout所有空间)pchks.validate();// 容器重绘(当容器内的东西由多变少时,防止多出来的部分没有清楚) ...
- 撑起大规模PHP网站的开源工具
撑起大规模PHP网站的开源工具 百万级PHP站点Poppen.de的架构 在 2011年11月27日 那天写的 已经有 3957 次阅读了 感谢 参考或原文 服务器君一共花费了54.510 ...
- Rails--render partial时传递参数
1. example如下: <%=render :partial => "networks/primary_agent", :locals => {:id =&g ...
- 自动配置IP地址.bat
※※※※※※※※※※※※※※※※※※※※※※※※※※※※ @echo ※ ※ @echo ...
- 20145317彭垚《Java程序设计》第3周学习总结
20145317彭垚<Java程序设计>第3周学习总结 教材学习内容总结 第四章 4.1类与对象 4.1.1定义类: new clothes():新建一个对象. class clothes ...
- 区分super和this
Java关键字this.super使用总结 一.this Java关键字this只能用于方法方法体内.当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 ...
- Memcache 提高缓存命中率
最近手上某个项目跟新代码,新的代码里大量采用memcahce作为缓存.所以开始深入了解memcache的内存分配策略.以前就听说有个PHP写的memcache监控脚本,在网上搜索了一下,果断下载下来用 ...
- Memory Allocation in the MySQL Server
https://dev.mysql.com/doc/internals/en/memory-allocation-mysql-server.html MySQL Internals Manual / ...
- java编程算法
一.字符串相关操作 String s = " Hello java,hello android,hello OOP,HELLO String,hello JAVASE!"; Sys ...