Android 上传文件到XP

Android部分:

Android 上传文件到XP
Android 上传文件到XP
              AsyncHttpClient client = new AsyncHttpClient();
                    RequestParams requestParams = new RequestParams();
                    File file = new File("/sdcard/DCIM/Camera/IMG_20140322_180445.jpg");
                    try {
                        requestParams.put("file", file);
                        requestParams.put("userid", "张三");
                    } catch (FileNotFoundException e) {
                        Log.d("T", e.getMessage());
                    }

                    client.post("http://192.168.30.30:9178/?fn=upload", requestParams,
                            new AsyncHttpResponseHandler() {
                                @Override
                                public void onSuccess(String response) {
                                    Log.d("T", response);
                                }

                                @Override
                                public void onFailure(Throwable error, String content) {
                                    Log.d("T", content);
                                    super.onFailure(error, content);
                                }
                            });
View Code
Android 上传文件到XP

服务端部分:

Android 上传文件到XP
Android 上传文件到XP
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HttpServer.HttpModules;
using System.Net;
using System.IO;
using System.Threading;
using HttpServer.FormDecoders;
using System.Collections.Concurrent;
namespace TestHttpServer
{
    public partial class Form1 : Form
    {
        private HttpServer.HttpServer _server = null;
        public Form1()
        {
            InitializeComponent();
            Console.WriteLine("UI:" + Thread.CurrentThread.ManagedThreadId);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _server = new HttpServer.HttpServer();
            _server.Add(new FileUpHttpModule());
            //_server.Add(new NotHandleModule());
            _server.Start(IPAddress.Any, int.Parse(textBox1.Text));
            Console.WriteLine("开启服务!");
        }
        protected override void OnClosed(EventArgs e)
        {
            UploadFileMonitor.Instance.OnFileUpload -= this.DisplayPic;
            if (_server != null)
            {
                _server.Stop();
                Console.WriteLine("关闭服务!");
            }
            base.OnClosed(e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new frmClient().Show();
        }
        private void DisplayPic(string filename)
        {
            if (this.InvokeRequired)
            {
                Console.WriteLine("需要异步调用!");
                this.Invoke( (Action) (() => { DisplayPic(filename); }));
            }
            else
            {
                pictureBox1.ImageLocation = filename;
                //pictureBox1.Refresh();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            UploadFileMonitor.Instance.OnFileUpload += this.DisplayPic;
        }
    }

    #region 上传监控
    public class UploadFileMonitor
    {
        const int C_MaxQueueLength = 100;
        public delegate void OnFileUploadHandler(string filename);
        private ConcurrentQueue<String> _Queue = new ConcurrentQueue<string>();
        public event OnFileUploadHandler OnFileUpload;
        private static readonly UploadFileMonitor _Instacne = new UploadFileMonitor();
        public static UploadFileMonitor Instance
        {
            get { return _Instacne; }
        }
        private UploadFileMonitor() { }

        public void Add(string filename)
        {
            _Queue.Enqueue(filename);
            while (_Queue.Count > C_MaxQueueLength +10)
            {

                String retrive = "";
                _Queue.TryDequeue(out retrive);
            }
            if (OnFileUpload != null)
            {
                try
                {
                    OnFileUpload(filename);
                }
                catch { }
            }
        }
    }
    #endregion
    public class FileUpHttpModule : HttpModule
    {

        public override bool Process(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session)
        {
            try
            {
                Console.WriteLine("work:" + Thread.CurrentThread.ManagedThreadId);
                var it = request.QueryString["fn"];
                Console.WriteLine("userId:" + request.Form["userid"].Value);
                Console.WriteLine(it);
                String filename = "";
                foreach (var file in request.Form.Files)
                {

                    filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.Ticks + Path.GetExtension(file.UploadFilename));
                    File.Copy(file.Filename, filename, true);
                    UploadFileMonitor.Instance.Add(filename);
                }
                StreamWriter writer = new StreamWriter(response.Body);
                if (string.IsNullOrWhiteSpace(filename))
                {
                    writer.WriteLine("no upload file!");
                }
                else
                {
                    writer.WriteLine(filename);
                }
                writer.Flush();

            
            }
            catch (Exception ex)
            {
                StreamWriter writer = new StreamWriter(response.Body);
                writer.WriteLine(ex.Message);
                writer.Flush();
            
            }
         
             return true;
            
        }
    }
}
View Code
Android 上传文件到XP

 

Android 上传文件到XP,布布扣,bubuko.com

Android 上传文件到XP

上一篇:Android 文件上传支持拍照录用录视频


下一篇:为App签名(为apk签名)