利用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样式文件和图片文件 ...
随机推荐
- JavaScript编码规范[百度]
JavaScript编码规范 1 前言 2 代码风格 2.1 文件 2.2 结构 2.2.1 缩进 2.2.2 空格 2.2.3 换行 2.2.4 语句 2.3 命名 ...
- FZU 1018 枚举dp
题意 给出一个数字组成的立方体 在其中选取一个体 使这个体中的数字之和最小 不可以不选 fzu的题目分类动态规划里面不是按难度排得 是按照题号..记得以前做题碰到过算 矩阵里面求子矩阵的最大和的 不会 ...
- [项目机会]citrix 虚拟桌面对于java等高CPU占用率如何解决
citrix 虚拟桌面对于java等高CPU占用率如何解决 问题1:java等客户端对于虚拟桌面cpu影响较大,但是有些用户的确需要使用java支持的程序,是否可以通过其他途径来解决? 问题2:对于其 ...
- 1st-code-review summary
每次做code review,先贤谆谆教诲便在耳畔响起: "There are only two hard problems in Computer Science: cache inval ...
- windows系统中ubuntu虚拟机安装及web项目到服务上(二)
ajp方式整合apache2和tomcat 7 1:在apache2.conf配置文件中启用模块mod_proxy_ajp,在里面添加 LoadModule proxy_module modules/ ...
- http响应状态码301和302
HTTP返回码中301与302的区别 (2012-10-15 22:06:09) 一.官方说法 301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redir ...
- 【转】Unity3D研究院之通过C#使用Advanced CSharp Messenger(五十)
http://www.xuanyusong.com/archives/2165 Advanced CSharp Messenger 属于C#事件的一种. 维基百科中由详细的说明http://wiki. ...
- Super不要在Super构造器中调用覆盖方法
import java.util.Date; public class Super{ public Super(){ System."); overrideMe(); System.&quo ...
- spring简单事务管理器
事务管理器 <!-- Transaction manager for a single JDBC DataSource --> <bean id="transaction ...
- java字符串和unicode互转
直接上代码 private static String decodeUnicode(String input) { if (null == input) return input; int len = ...