服务端:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.IO;namespace AA.Web.Controllers{    public class UploadController : Controller    {        //        // GET: /Upload/        public ActionResult Up()        {            var fname = Request.Form["fname"];            if (Request.Files.Count > 0)            {                var file = Request.Files[0];                var filename=Guid.NewGuid().ToString("N") +  Path.GetExtension(file.FileName);                file.SaveAs(Server.MapPath("/" + filename));            }            return Content(fname);        }    }}
View Code

客户端:

public static String uploadFile(String filename){        try {            // "http://192.168.1.7:7086/account/getUsers";//            String httpUrl = "http://192.168.1.7:7086/upload/up";            // HttpGet连接对象            HttpPost httpRequest = new HttpPost(httpUrl);            // 取得HttpClient对象            HttpClient httpClient = new DefaultHttpClient();                        MultipartEntity mEntity=new MultipartEntity();                         ContentBody cbFile=new FileBody(new File(filename));            mEntity.addPart("file1", cbFile);            mEntity.addPart("fname", new StringBody("这是一个几吧文件","text/plan", Charset.forName("utf-8")));                                    httpRequest.setEntity(mEntity);                                                // 请求HttpClient,取得HttpResponse            HttpResponse httpResponse = httpClient.execute(httpRequest,httpContext);                    // 请求成功            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                // 取得返回的字符串                String strResult = EntityUtils.toString(httpResponse.getEntity());                httpClient.getConnectionManager().shutdown();                return strResult;            }             throw new RuntimeException("网络请求执行错误!");                    } catch (Exception e) {            throw new RuntimeException(e);        }    }
View Code

需要添加httpmime-4.1.1.jar文件

更多相关文章

  1. Android(安卓)桌面快捷方式操作
  2. android WebView结合javascript相互调用
  3. Android(安卓)跳转Activity并通过Bundle对象传递数据
  4. Android(安卓)定时到服务器取数据并刷新
  5. Android创建和删除桌面快捷方式
  6. android之获得当前连接wifi的名字
  7. android WebView结合javascript相互调用
  8. Android(安卓)App怎样调用 Frameworks Bluetooth接口
  9. android添加通知到顶部任务栏

随机推荐

  1. 中国人寿自动化运维自主研发之路
  2. postgresql copy使用
  3. 「面试高频」秒杀架构的设计套路,你值得拥
  4. 苹果Mac如何设置应用默认打开方式?
  5. DevOps绞杀者之路
  6. 使用开源软件打造类似Google的开发和生产
  7. shell实现MySQL全量备份
  8. nginx负载均衡中的session共享
  9. 使用106短信群发有那些优势?
  10. 干货|如何步入Service Mesh微服务架构时