Silverlight技术调查(4)——完成的调查结果
原文 Silverlight技术调查(4)——完成的调查结果
客户端使用Silverlight+DXperience,可以在线编辑各种常见文本及富文本文档(doc、docx、rtf、txt、html……),Silverlight使用了异步提交和响应(通过WebClient和WebRequest),提交时,自行编排HTTP请求格式(请参见相应RFC标准);编辑的数据流上传至Servlet后,Servlet使用commons-fileupload-1.2.2解包数据,获取附加的请求参数及附件内容。
程序效果:
下面为客户端和服务器端程序:
(一)客户端Silverlight由部分组成:
1、主程序画面Layout部分MainPage.xaml:
- <UserControl x:Class="RichEdit.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="400" xmlns:dxre="http://schemas.devexpress.com/winfx/2008/xaml/richedit" xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon">
- <UserControl.Resources>
- <ResourceDictionary>
- <dxre:RichEditUICommand x:Key="commands" />
- <dxre:RichEditStringIdConverter x:Key="stringIdConverter" />
- </ResourceDictionary>
- </UserControl.Resources>
- <Grid x:Name="LayoutRoot" Background="White">
- <dxb:BarManager Name="barManager1" ToolbarGlyphSize="Small">
- <dxb:BarManager.Items>
- <dxb:BarButtonItem Command="{Binding Path=FileNew, Mode=OneTime, Source={StaticResource commands}}" Name="biFileNew" />
- <dxb:BarButtonItem Command="{Binding Path=FileSaveAs, Mode=OneTime, Source={StaticResource commands}}" Name="biFileSaveAs" />
- <dxb:BarButtonItem Command="{Binding Path=FilePrint, Mode=OneTime, Source={StaticResource commands}}" Name="biFilePrint" />
- <dxb:BarButtonItem Command="{Binding Path=FilePrintPreview, Mode=OneTime, Source={StaticResource commands}}" Name="biFilePrintPreview" />
- <dxb:BarButtonItem Command="{Binding Path=FileBrowserPrint, Mode=OneTime, Source={StaticResource commands}}" Name="biFileBrowserPrint" />
- <dxb:BarButtonItem Command="{Binding Path=FileBrowserPrintPreview, Mode=OneTime, Source={StaticResource commands}}" Name="biFileBrowserPrintPreview" />
- <dxb:BarButtonItem Command="{Binding Path=EditUndo, Mode=OneTime, Source={StaticResource commands}}" Name="biEditUndo" />
- <dxb:BarButtonItem Command="{Binding Path=EditRedo, Mode=OneTime, Source={StaticResource commands}}" Name="biEditRedo" />
- <dxb:BarButtonItem Content="LoadFromWeb" Name="barButtonItemLoad" Glyph="/RichEdit;component/Images/Chrysanthemum.jpg" ItemClick="barButtonItemLoad_ItemClick" GlyphAlignment="Top" GlyphSize="Default" />
- <dxb:BarButtonItem Name="barButtonItemSave" Glyph="/RichEdit;component/Images/Desert.jpg" Content="SaveToWeb" ItemClick="barButtonItemSave_ItemClick" />
- <dxb:BarStaticItem Content="message..." Name="barStaticItemMsg" />
- </dxb:BarManager.Items>
- <dx:DockPanel>
- <dxr:RibbonControl dx:DockPanel.Dock="Top" Name="ribbonControl1">
- <dxr:RibbonControl.ApplicationMenu>
- <dxr:ApplicationMenuInfo />
- </dxr:RibbonControl.ApplicationMenu>
- <dxr:RibbonDefaultPageCategory>
- <dxr:RibbonPage Caption="{Binding Source={StaticResource stringIdConverter}, ConverterParameter=Caption_PageFile, Converter={StaticResource stringIdConverter}, Mode=OneTime}" Name="pageFile">
- <dxr:RibbonPageGroup Caption="{Binding Source={StaticResource stringIdConverter}, ConverterParameter=Caption_GroupCommon, Converter={StaticResource stringIdConverter}, Mode=OneTime}" Name="grpCommon" ShowCaptionButton="False">
- <dxb:BarButtonItemLink BarItemName="biFileNew" />
- <dxb:BarButtonItemLink BarItemName="biFileSaveAs" />
- <dxb:BarButtonItemLink BarItemName="biFilePrint" />
- <dxb:BarButtonItemLink BarItemName="biFilePrintPreview" />
- <dxb:BarButtonItemLink BarItemName="biFileBrowserPrint" />
- <dxb:BarButtonItemLink BarItemName="biFileBrowserPrintPreview" />
- <dxb:BarButtonItemLink BarItemName="biEditUndo" />
- <dxb:BarButtonItemLink BarItemName="biEditRedo" />
- </dxr:RibbonPageGroup>
- <dxr:RibbonPageGroup Caption="Ribbon Page Group 1" Name="ribbonPageGroup1">
- <dxb:BarButtonItemLink BarItemName="barButtonItemLoad" />
- <dxb:BarButtonItemLink BarItemName="barButtonItemSave" />
- </dxr:RibbonPageGroup>
- <dxr:RibbonPageGroup Caption="Ribbon Page Group 2" Name="ribbonPageGroup2">
- <dxb:BarStaticItemLink BarItemName="barStaticItemMsg" />
- </dxr:RibbonPageGroup>
- </dxr:RibbonPage>
- </dxr:RibbonDefaultPageCategory>
- </dxr:RibbonControl>
- <dxre:RichEditControl Name="richEditControl1" BarManager="{Binding ElementName=barManager1, Mode=OneTime}" Ribbon="{Binding ElementName=ribbonControl1, Mode=OneTime}" />
- </dx:DockPanel>
- </dxb:BarManager>
- </Grid>
- </UserControl>
2、主程序逻辑部分MainPage.xaml.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.IO;
- using DevExpress.XtraRichEdit;
- using System.Threading;
- namespace RichEdit
- {
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- }
- String uri = "http://localhost:8888/upload/UploadServlet";
- private void barButtonItemLoad_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
- {
- MessageBox.Show("Load Begin");
- ServerStreamLoad ssl = new ServerStreamLoad();
- ssl.Rp = new ShowLoadResultDelegate(ShowLoadResult);
- ssl.Load(uri);
- }
- private void barButtonItemSave_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
- {
- MessageBox.Show("Save Begin");
- WebRequest request = WebRequest.Create(new Uri(uri));
- Dictionary<String, String> nvc = new Dictionary<String, String>();
- nvc.Add("name", "value");
- nvc.Add("name1", "value1");
- ServerStreamSave sss = new ServerStreamSave();
- sss.Rp = new ShowSaveResultDelegate(ShowSaveResult);
- Stream stream = new MemoryStream();
- this.richEditControl1.SaveDocument(stream, DocumentFormat.Rtf);
- sss.Save(request, nvc, stream);
- }
- // 处理Load结果
- public void ShowLoadResult(Stream stream)
- {
- String msg = "OK";
- if (this.barStaticItemMsg.CheckAccess())
- {
- this.barStaticItemMsg.Content = msg;
- }
- else
- {
- Dispatcher.BeginInvoke(() =>
- {
- this.barStaticItemMsg.Content = msg;
- });
- }
- using (stream)
- {
- if (this.richEditControl1.CheckAccess())
- {
- this.richEditControl1.LoadDocument(stream, DocumentFormat.Rtf);
- }
- else
- {
- Dispatcher.BeginInvoke(() =>
- {
- this.richEditControl1.LoadDocument(stream, DocumentFormat.Rtf);
- });
- }
- }
- }
- // 处理Save结果
- public void ShowSaveResult(String msg)
- {
- if (this.barStaticItemMsg.CheckAccess())
- {
- this.barStaticItemMsg.Content = msg;
- }
- else
- {
- Dispatcher.BeginInvoke(() =>
- {
- this.barStaticItemMsg.Content = msg;
- });
- }
- }
- }
- // 处理结果的委托
- public delegate void ShowLoadResultDelegate(Stream stream);
- public delegate void ShowSaveResultDelegate(String s);
- }
3、数据流异步下载部分ServerStreamLoad.cs:
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.IO;
- namespace RichEdit
- {
- public class ServerStreamLoad
- {
- ShowLoadResultDelegate rp;
- public ShowLoadResultDelegate Rp
- {
- get { return rp; }
- set { rp = value; }
- }
- public void Load(String uri)
- {
- WebClient webClient = new WebClient();
- webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadCompletedCallBack);
- webClient.OpenReadAsync(new Uri(uri));
- }
- private void LoadCompletedCallBack(object sender, OpenReadCompletedEventArgs e)
- {
- using (Stream stream = e.Result)
- {
- // read stream here
- rp(stream);
- }
- }
- }
- }
4、数据流异步提交部分:
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.IO;
- using System.Collections.Generic;
- using System.Windows.Threading;
- namespace RichEdit
- {
- public class ServerStreamSave
- {
- static ShowSaveResultDelegate rp;
- public ShowSaveResultDelegate Rp
- {
- get { return rp; }
- set { rp = value; }
- }
- public void Save(WebRequest request, Dictionary<String, String> nvc, Stream stream)
- {
- request.Method = "POST";
- UploadState us = new UploadState();
- us.Req = request;
- us.Nvc = nvc;
- us.Stream = stream;
- request.BeginGetRequestStream(new AsyncCallback(SaveRequestReadyCallBack), us);
- }
- private void SaveRequestReadyCallBack(IAsyncResult asyncResult)
- {
- string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
- byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
- WebRequest request = ((UploadState)(asyncResult.AsyncState)).Req as WebRequest;
- request.ContentType = "multipart/form-data; boundary=" + boundary;
- request.UseDefaultCredentials = true;
- Stream rs = request.EndGetRequestStream(asyncResult);
- // 1.write boundery and key-value pairs
- string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
- IDictionary<String, String> nvc = ((UploadState)(asyncResult.AsyncState)).Nvc as IDictionary<String, String>;
- foreach (string key in nvc.Keys)
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string formitem = string.Format(formdataTemplate, key, nvc[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- rs.Write(formitembytes, 0, formitembytes.Length);
- }
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- // 2.write head
- string file = "file";
- string paramName = "file";
- string contentType = "unknown";
- string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
- string header = string.Format(headerTemplate, paramName, file, contentType);
- byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
- rs.Write(headerbytes, 0, headerbytes.Length);
- // 3.write stream here,the stream will be closed by invoker
- using (Stream stream = ((UploadState)(asyncResult.AsyncState)).Stream as Stream)
- {
- stream.Position = 0;
- //byte[] buffer = System.Text.Encoding.UTF8.GetBytes("aaa我11");
- byte[] data = new byte[4096];
- int count = stream.Read(data, 0, 4096);
- while (count > 0)
- {
- rs.Write(data, 0, count);
- count = stream.Read(data, 0, 4096);
- }
- }
- //rs.Write(buffer, 0, buffer.Length);
- // 4.write trailer
- byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
- rs.Write(trailer, 0, trailer.Length);
- rs.Flush();
- rs.Close();
- request.BeginGetResponse(new AsyncCallback(SaveResponseReadyCallBack), request);
- }
- private void SaveResponseReadyCallBack(IAsyncResult asyncResult)
- {
- WebRequest request = asyncResult.AsyncState as WebRequest;
- WebResponse response = request.EndGetResponse(asyncResult);
- using (Stream responseStream = response.GetResponseStream())
- {
- StreamReader reader = new StreamReader(responseStream);
- // check response result by return msg.
- String msg = reader.ReadToEnd();
- rp(msg);
- }
- }
- }
- }
5、异步请求提交,回调封装参数部分UploadState.cs:
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- namespace RichEdit
- {
- public class UploadState
- {
- IDictionary<String, String> nvc = null;
- public IDictionary<String, String> Nvc
- {
- get { return nvc; }
- set { nvc = value; }
- }
- WebRequest req = null;
- public WebRequest Req
- {
- get { return req; }
- set { req = value; }
- }
- Stream stream;
- public Stream Stream
- {
- get { return stream; }
- set { stream = value; }
- }
- }
- }
(二)服务器端使用commons-fileupload-1.2.2.jar实现上传:
1、web.xml配置:
- <servlet>
- <display-name>UploadServlet</display-name>
- <servlet-name>UploadServlet</servlet-name>
- <servlet-class>com.liyj.upload.servlet.UploadServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UploadServlet</servlet-name>
- <url-pattern>/UploadServlet</url-pattern>
- </servlet-mapping>
2、UploadServlet.java(取打包的参数和附件)
- package com.liyj.upload.servlet;
- import java.io.*;
- import java.util.List;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.commons.fileupload.FileUploadException;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- public class UploadServlet extends HttpServlet {
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response) {
- ServletOutputStream out = null;
- System.out.println("into doGet..");
- try {
- out = response.getOutputStream();
- FileInputStream fis = new FileInputStream("D:\\file");
- byte[] data = new byte[4096];
- int count = fis.read(data);
- while(count > 0) {
- out.write(data, 0, count);
- count = fis.read(data);
- }
- fis.close();
- out.flush();
- out.close();
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response) {
- ServletOutputStream out = null;
- System.out.println("into doPost..");
- DiskFileItemFactory factory = new DiskFileItemFactory();
- String path = "D:\\";
- factory.setRepository(new File(path));
- factory.setSizeThreshold(1024 * 1024);
- ServletFileUpload upload = new ServletFileUpload(factory);
- try {
- out = response.getOutputStream();
- List<FileItem> list = upload.parseRequest(request);
- for (FileItem item : list) {
- if (item.isFormField()) {
- String name = item.getFieldName();
- String value = item.getString("gbk");
- out.println(name + ":" + value);
- } else {
- String name = item.getFieldName();
- String value = item.getName();
- int start = value.lastIndexOf("\\");
- String fileName = value.substring(start + 1);
- //request.setAttribute(name, fileName);
- OutputStream os = new FileOutputStream(new File(path, fileName));
- InputStream is = item.getInputStream();
- byte[] buffer = new byte[1024];
- int length = 0;
- int fileLen = 0;
- while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
- fileLen += length;
- }
- out.println("OK");
- os.close();
- is.close();
- }
- }
- } catch (Exception e) {
- try {
- out.println("NG");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- }
- }
---------- THE END-----------
Silverlight技术调查(4)——完成的调查结果的更多相关文章
- Silverlight技术调查(3)——国际化
原文 Silverlight技术调查(3)——国际化 网上有很多关于Silverlight国际化的说明,包括MSDN的示例,都没有强调一点,下面以红色标示,基础国际化知识请先参考MSDN相关章节,关键 ...
- Silverlight技术调查(2)——跨域访问
原文 Silverlight技术调查(2)——跨域访问 此调查web容器采用的是Tomcat,若允许所有域访问,只需在webapps下的根应用ROOT中,加入配置文件:clientaccesspoli ...
- Silverlight技术调查(1)——Html向Silverlight传参
原文 Silverlight技术调查(1)——Html向Silverlight传参 近几日项目研究一个很牛的富文档编辑器DXperience RichEdit组件,调查环境为Silverlight4. ...
- Python技术调查
1. IDE 2. Local Debugging & Remote Debugging 3. Profiling
- .NET Web开发技术简单整理
在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...
- CTO和技术副总裁应该如何分工?谁才是技术领导者?
谁是初创公司的技术领导者,是CTO还是技术副总裁?任何在创业公司工作的人都知道,我们不应该去问这个问题.因为这两个是非常不同的角色,角色本身会随着创业公司的发展而变化,两者对于业务规模都很重要. 简单 ...
- 使用SilverLight开发区域地图分析模块
本人最近接收开发一个代码模块,功能主要是在页面上显示安徽省市地图,并且在鼠标移动到地图某市区域时,显示当前区域的各类信息等,一开始准备用百度地图,高德地图等地图工具进行开发,最后发现都不适合进行此类开 ...
- Silverlight开发工具汇总
随着Silverlight技术的逐步完善,Silverlight应用大批的涌现,近期的2010年冬季奥运会,Silverlight作为首选视频播放技术,为全球提供在线赛事实况. Silverlight ...
- 转载:.NET Web开发技术简单整理
在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...
随机推荐
- java 简单的数据增删该查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- 循环调用修正sic86 2改后的(除了第一年有点诡异,其他年份可以正常修复)
create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...
- ELK 之二:ElasticSearch 和Logstash高级使用
一:文档 官方文档地址:1.x版本和2.x版本 https://www.elastic.co/guide/en/elasticsearch/guide/index.html 硬件要求: 1.内存,官方 ...
- 【Flume NG用户指南】(1)设置
作者:周邦涛(Timen) Email:zhoubangtao@gmail.com 转载请注明出处: http://blog.csdn.net/zhoubangtao/article/details ...
- MSSQL奇技淫巧
MSSQL:获得库每个表的记录数和容量 sp_msforeachtable是MS未公开的存储过程: exec sp_msforeachtable @command1="print '?'&q ...
- ASP.NET Excel导入到SQL Server数据库
本文转自:http://www.cnblogs.com/lhking/archive/2009/06/08/1499002.html 提供把Excel里的数据导入到SQL Server 数据库,前提是 ...
- HDOJ 1272 并查集 不相同父节点
判断两点:1.任何2点的父节点不能相同->否则会导致2点间有多条通路2.所有点只有1个集合 存在一个小坑,就是第一次输入 0 0 的时候,应该输出 Yes , 否则会WA MY AC Code ...
- java.util.concurrent-------TimeUnit
java.util.concurrent并发库是JDK1.5新增的,其作者是Doug Lea ,此人是个BOSS级别的天才人物了.有了他提供的类库,使得我们对多线程并发.锁有了很大的帮助,减少了并发难 ...
- 黑龙江省第七届大学生程序设计竞赛-Heap
描述 A heap is a full binary tree; for each node, its key is greater than its two sub-node’s key. Two ...
- Spring源代码解析 ---- 循环依赖
一.循环引用: 1. 定义: 循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方,比方CircularityA引用CircularityB,CircularityB引用Circularit ...