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);
}
});

服务端部分:

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 = ;
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 +)
{ 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; }
}
}

Android 上传文件到XP的更多相关文章

  1. android上传文件到服务器

    package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...

  2. android -上传文件到服务器

    android上传文件到服务器       重点:最好是设置好content-type这些参数的配置!     package com.spring.sky.image.upload.network; ...

  3. android 上传文件

    android对于上传文件,还是非常easy的,和java里面的上传都是一样的,基本上都是熟悉操作输出流和输入流!另一个特别重要的就是须要一些content-type这些參数的配置!  假设这些都弄好 ...

  4. Android上传文件至服务器(上)

    每一次都不能上首页,真悲催..管理员让我上一次首页? 很多时候我更愿意一个人写代码,与其在垃圾代码上改改改,我更愿意直接重构. 整洁的代码简单直接.整洁的代码如同优美的散文.整洁的代码从不隐藏设计者的 ...

  5. android上传文件到wamp服务器

    1.php server(wamp)部分 建立unload.php页面代码如下 <?php move_uploaded_file($_FILES["file1"][" ...

  6. 【经验记录】Android上传文件到服务器

    Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...

  7. Android上传文件到服务器(转)

    Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...

  8. Android上传文件之FTP

    android客户端实现FTP文件(包括图片)上传应该没什么难度.写下来就了为了记录一下,望能帮到新手. 需要用到 commons-net-3.0.1.jar,后面附上jar包. 直接上代码: /** ...

  9. Android 上传文件到 FTP 服务器

    实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...

随机推荐

  1. C# 能否获取一个对象所占内存的大小?

    今日,在项目重构的时候忽然想到一个问题,一个类哪些成员的增加,会影响一个类所占内存的大小?C#有没有办法知道一个对象占多少内存呢? 第一个问题:很快想到是类的非静态的字段.属性. 第二个问题:首先想到 ...

  2. hBuilder培训资源视频教程汇总

    DCloud对开发者的学习支持分3个层面:官方文档.三方专业培训.网友经验分享 DCloud的精力主要在做产品,配套的文档也会一直完善好.但专业的培训还不是DCloud能做好的,在HTML5中国产业联 ...

  3. ACM-Teleportation

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { int a,b,x,y; cin>>a> ...

  4. java websocket @ServerEndpoint注解说明

    http://www.blogjava.net/qbna350816/archive/2016/07/24/431302.html https://segmentfault.com/q/1010000 ...

  5. 【ZZ】Web开发的入门指导 | 菜鸟教程

    Web开发的入门指导 http://www.runoob.com/w3cnote/a-beginners-guide-to-web-development.html

  6. mysql-7事务管理

    1.事务的使用场景 mysql事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人愿,你既需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...

  7. 如何制作行政区划矢量图(shp格式)

    详细图文ArcGIS10.2破解版教程地址:http://jingyan.baidu.com/article/e73e26c0cb5c1324adb6a791.html 有时候想要一张shp格式的地方 ...

  8. python写exploit采集器

    前言: 根据天文地理与风水学,我掐指一算的看到了一篇不错的文章,文章里面写到整理exploit 我顿时心理想写一个exploit采集器,那么说时迟那时快.就开始写了 代码: 思路:http://exp ...

  9. C++中构造函数作用

    一. 构造函数是干什么的 class Counter { public: // 类Counter的构造函数 // 特点:以类名作为函数名,无返回类型 Counter() { m_value = ; } ...

  10. 人脸识别-<转>

    人脸检测库libfacedetection介绍 libfacedetection是于仕琪老师放到GitHub上的二进制库,没有源码,它的License是MIT,可以商用.目前只提供了windows 3 ...