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. web程序1

  2. 使用 Content-Encoding: br 替换 Content-Encoding: gzip

    今天在测试一个web 框架的时候无意发现框架运行是响应头时有一个 Content-Encoding: br 发现是一个gzip 算法的替代,同时压缩比很不错 浏览器兼容性如下: nginx 的配置参考 ...

  3. python连接redis sentinel集群

    安装 python redis 客户端 pip install redis #!/usr/bin/env python # -*- coding:utf-8 -*- #!/usr/bin/env py ...

  4. centos7下Firewall使用详解

    安装它,只需 # yum install firewalld如果需要图形界面的话,则再安装# yum install firewall-config 一.介绍 防火墙守护 firewalld 服务引入 ...

  5. 洛谷2148(SDOI2009) E&D

    题目:https://www.luogu.org/problemnew/show/P2148 SG函数+找规律. 普通地用SG函数做. 每两堆是一个独立问题.因为虽然有可能一个人在同一组里连续操作2次 ...

  6. linux系统报错日志学习

    linux本身会自动记录系统报错日志:/var/log/messages 这个日志记录,我是在什么时候发现其强大的作用的呢?它有点像我们使用php脚本开发接口的时候技术员在重要地方打日志的效果,方便技 ...

  7. 黄聪:C#使用能够foreach对hashtable、List遍历时“集合已修改;可能无法执行枚举操作。”错误

    解决办法:使用for循环,而不是foreach循环 例如: ArrayList akeys=new ArrayList(_transmit_tb.Keys); ;p> -;p--) { _tra ...

  8. Java 基于UDP的类似于QQ的循环通信

    package Day10; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetS ...

  9. HDU 4762 Cut the Cake(高精度)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  10. Flask框架的学习与实战(一):开发环境搭建

    Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2.很多功能的实现都参考了django框架.由于项目需要,在 ...