zoukankan      html  css  js  c++  java
  • Android 上传文件到XP

    Android部分:

                  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

    服务端部分:

    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
  • 相关阅读:
    java学习之实例变量初始化
    rip中的连续子网以及不连续子网
    扫描工具
    WScript.SendKeys()的sendkeys发送组合键以及特殊字符
    sql 查询包含字符的数量统计
    leetcode题1Two sum 练习
    vs 2015密钥
    前端 边界圆角
    前端 字体样式
    前端 高级选择器 伪类选择器
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3680677.html
Copyright © 2011-2022 走看看