Silverlight技术调查(4)——完成的调查结果

Silverlight技术调查(4)——完成的调查结果,第1张

概述客户端使用Silverlight+DXperience,可以在线编辑各种常见文本及富文本文档(doc、docx、rtf、txt、html……),Silverlight使用了异步提交和响应(通过WebClient和WebRequest),提交时,自行编排HTTP请求格式(请参见相应RFC标准);编辑的数据流上传至Servlet后,Servlet使用commons-fileupload-1.2.2解包

客户端使用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,Source={StaticResource commands}}" name="bifileSaveAs" />                <dxb:barbuttonItem Command="{Binding Path=filePrint,Source={StaticResource commands}}" name="bifilePrint" />                <dxb:barbuttonItem Command="{Binding Path=filePrintPrevIEw,Source={StaticResource commands}}" name="bifilePrintPrevIEw" />                <dxb:barbuttonItem Command="{Binding Path=filebrowserPrint,Source={StaticResource commands}}" name="bifilebrowserPrint" />                <dxb:barbuttonItem Command="{Binding Path=filebrowserPrintPrevIEw,Source={StaticResource commands}}" name="bifilebrowserPrintPrevIEw" />                <dxb:barbuttonItem Command="{Binding Path=EditUndo,Source={StaticResource commands}}" name="bIEditUndo" />                <dxb:barbuttonItem Command="{Binding Path=EditRedo,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,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,boundarybytes.Length);                string formitem = string.Format(formdataTemplate,key,nvc[key]);                byte[] formitembytes = System.Text.EnCoding.UTF8.GetBytes(formitem);                rs.Write(formitembytes,formitembytes.Length);            }            rs.Write(boundarybytes,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,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,4096);                while (count > 0)                {                    rs.Write(data,count);                    count = stream.Read(data,4096);                }            }            //rs.Write(buffer,buffer.Length);            // 4.write trailer            byte[] trailer = System.Text.EnCoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");            rs.Write(trailer,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>	<@R_404_1674@>		<servlet-name>UploadServlet</servlet-name>		<url-pattern>/UploadServlet</url-pattern>	</@R_404_1674@>

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,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,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技术调查(4)——完成的调查结果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073739.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存