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

    服务端:

    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文件

  • 相关阅读:
    Python多线程之死锁
    Python多线程之间同步总结
    C语言动态数组
    Python标准库 -- UUID模块(生成唯一标识)
    东南亚 SAP 实施 马来西亚税收在SAP的设计和实现
    odoo:开源 ERP/CRM 入门与实践 -- 上海嘉冰信息技术公司提供咨询服务
    SAP FICO 凭证导入接口 数据xml格式
    SAP HANA S4 FI TABLE表结构
    CFA一级知识点总结
    汽车行业与 Telematics
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3273001.html
Copyright © 2011-2022 走看看