Android实现表单提交,webapi接收

1.服务端采用的是.net的WEBAPI接口。

2.android多文件上传。

以下为核心代码:

  1 package com.example.my.androidupload;
  2
  3
  4 import android.util.Log;
  5
  6 import java.io.File;
  7 import java.io.Serializable;
  8 import java.util.Iterator;
  9 import java.util.LinkedHashMap;
 10 import java.util.Map;
 11 import java.util.Map.Entry;
 12
 13 import org.apache.http.Consts;
 14 import org.apache.http.HttpEntity;
 15 import org.apache.http.HttpResponse;
 16 import org.apache.http.client.HttpClient;
 17 import org.apache.http.client.methods.HttpPost;
 18 import org.apache.http.entity.ContentType;
 19 import org.apache.http.entity.mime.MultipartEntityBuilder;
 20 import org.apache.http.entity.mime.content.FileBody;
 21 import org.apache.http.entity.mime.content.StringBody;
 22 import org.apache.http.impl.client.DefaultHttpClient;
 23 import org.apache.http.util.EntityUtils;
 24
 25 /**
 26  * Created by wz on 2016/04/19.
 27  */
 28 public class HttpClientUtil {
 29     public final static String Method_POST = "POST";
 30     public final static String Method_GET = "GET";
 31
 32     /**
 33      * multipart/form-data类型的表单提交
 34      *
 35      * @param form 表单数据
 36      */
 37     public static String submitForm(MultipartForm form) {
 38         // 返回字符串
 39         String responseStr = "";
 40
 41         // 创建HttpClient实例
 42 /*        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
 43         CloseableHttpClient httpClient = httpClientBuilder.build();*/
 44         // CloseableHttpClient httpClient= HttpClients.createDefault();
 45
 46         HttpClient httpClient = new DefaultHttpClient();
 47         try {
 48             // 实例化提交请求
 49             HttpPost httpPost = new HttpPost(form.getAction());
 50
 51             // 创建MultipartEntityBuilder
 52             MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
 53
 54             Log.e("HttpClientUtil", "111");
 55             // 追加普通表单字段
 56             Map<String, String> normalFieldMap = form.getNormalField();
 57             for (Iterator<Entry<String, String>> iterator = normalFieldMap.entrySet().iterator(); iterator.hasNext(); ) {
 58                 Entry<String, String> entity = iterator.next();
 59                 entityBuilder.addPart(entity.getKey(), new StringBody(entity.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
 60             }
 61             Log.e("HttpClientUtil", "222");
 62             // 追加文件字段
 63             Map<String, File> fileFieldMap = form.getFileField();
 64             for (Iterator<Entry<String, File>> iterator = fileFieldMap.entrySet().iterator(); iterator.hasNext(); ) {
 65                 Entry<String, File> entity = iterator.next();
 66                 entityBuilder.addPart(entity.getKey(), new FileBody(entity.getValue()));
 67             }
 68             Log.e("HttpClientUtil", "333");
 69             // 设置请求实体
 70             httpPost.setEntity(entityBuilder.build());
 71             Log.e("HttpClientUtil", "444");
 72             // 发送请求
 73             HttpResponse response = httpClient.execute(httpPost);
 74             int statusCode = response.getStatusLine().getStatusCode();
 75             // 取得响应数据
 76             HttpEntity resEntity = response.getEntity();
 77             Log.e("HttpClientUtil", "结果:" + Integer.toString(statusCode));
 78             if (200 == statusCode) {
 79                 if (resEntity != null) {
 80                     responseStr = EntityUtils.toString(resEntity);
 81                 }
 82             }
 83         } catch (Exception e) {
 84             System.out.println("提交表单失败,原因:" + e.getMessage());
 85         } finally {
 86             try {
 87                 httpClient.getConnectionManager().shutdown();
 88             } catch (Exception ex) {
 89             }
 90         }
 91         return responseStr;
 92     }
 93
 94     /**
 95      * 表单字段Bean
 96      */
 97     public class MultipartForm implements Serializable {
 98         /**
 99          * 序列号
100          */
101         private static final long serialVersionUID = -2138044819190537198L;
102
103         /**
104          * 提交URL
105          **/
106         private String action = "";
107
108         /**
109          * 提交方式:POST/GET
110          **/
111         private String method = "POST";
112
113         /**
114          * 普通表单字段
115          **/
116         private Map<String, String> normalField = new LinkedHashMap<String, String>();
117
118         /**
119          * 文件字段
120          **/
121         private Map<String, File> fileField = new LinkedHashMap<String, File>();
122
123         public String getAction() {
124             return action;
125         }
126
127         public void setAction(String action) {
128             this.action = action;
129         }
130
131         public String getMethod() {
132             return method;
133         }
134
135         public void setMethod(String method) {
136             this.method = method;
137         }
138
139         public Map<String, String> getNormalField() {
140             return normalField;
141         }
142
143         public void setNormalField(Map<String, String> normalField) {
144             this.normalField = normalField;
145         }
146
147         public Map<String, File> getFileField() {
148             return fileField;
149         }
150
151         public void setFileField(Map<String, File> fileField) {
152             this.fileField = fileField;
153         }
154
155         public void addFileField(String key, File value) {
156             fileField.put(key, value);
157         }
158
159         public void addNormalField(String key, String value) {
160             normalField.put(key, value);
161         }
162     }
163
164
165 }

调用:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mBtnUpload;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_upload: {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpClientUtil httpClient = new HttpClientUtil();
                        HttpClientUtil.MultipartForm form = httpClient.new MultipartForm();
                        //设置form属性、参数
                        form.setAction("http://192.168.1.5:9001/api/Mobile/FileUpload/UploadFile");
                        String path= Environment.getExternalStorageDirectory().getPath()+"/DCIM" + "/20151120_051052.jpg";
                        Log.e("11", path);
                        File file = new File(path);
                        form.addFileField("file", file);
                        form.addNormalField("ID", "301201604");
                        String resultcode = httpClient.submitForm(form);

                        Log.e("上传结果:",resultcode);
                    }
                }).start();

                break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtnUpload = (Button) findViewById(R.id.btn_upload);
        mBtnUpload.setOnClickListener(this);
    }
}

服务端代码(由.net WEBAPI开发):

  1     /// <summary>
  2     /// 附件上传
  3     /// </summary>
  4     [RoutePrefix("api/Mobile/FileUpload")]
  5     public class FileUploadController : ApiController
  6     {
  7         static readonly string UploadFolder = "/UpLoad/ApiUpload";
  8         static readonly string ServerUploadFolder = HttpContext.Current.Server.MapPath(UploadFolder);
  9
 10
 11         #region 上传文件-文件信息不保存到数据库
 12         /// <summary>
 13         /// 上传文件-文件信息不保存到数据库
 14         /// </summary>
 15         /// <returns>返回文件信息</returns>
 16         [HttpPost]
 17         [Route("UploadFile")]
 18         public async Task<List<FileResultDTO>> UploadFile()
 19         {
 20             Log.Info("UploadFile", "进入");
 21
 22             // 验证这是一个HTML表单文件上传请求
 23             if (!Request.Content.IsMimeMultipartContent("form-data"))
 24             {
 25                 Log.Info("UploadFile", "error01");
 26                 throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
 27             }
 28
 29
 30             // Create a stream provider for setting up output streams
 31             MultipartFormDataStreamProvider streamProvider = new RenamingMultipartFormDataStreamProvider(ServerUploadFolder);
 32             // Read the MIME multipart asynchronously content using the stream provider we just created.
 33             await Request.Content.ReadAsMultipartAsync(streamProvider);
 34             Log.Info("UploadFile", "进入01");
 35             List<FileResultDTO> itemList = new List<FileResultDTO>();
 36             foreach (MultipartFileData file in streamProvider.FileData)
 37             {
 38                 Log.Info("UploadFile", "进入02");
 39
 40                 FileInfo fileInfo = new FileInfo(file.LocalFileName);
 41
 42                 FileResultDTO item = new FileResultDTO();
 43                 var sLocalFileName = file.LocalFileName;
 44                 string fileName = sLocalFileName.Substring(sLocalFileName.LastIndexOf("\\") + 1, sLocalFileName.Length - sLocalFileName.LastIndexOf("\\") - 1);
 45
 46                 item.ID = Guid.NewGuid();
 47                 item.FileName = file.Headers.ContentDisposition.FileName;
 48                 item.ServerFileName = fileInfo.Name;
 49                 item.FileSize = fileInfo.Length;
 50                 item.FilePath = UploadFolder;
 51                 item.FullFilePath = ServerUploadFolder;
 52                 item.ContentType = file.Headers.ContentType.MediaType.ToString();
 53                 item.UploadTime = DateTime.Now;
 54
 55                 itemList.Add(item);
 56             }
 57             Log.Info("UploadFile", "进入03");
 58             return itemList;
 59         }
 60         #endregion
 61
 62         #region 上传文件-文件信息会保存到数据库
 63
 64         /// <summary>
 65         /// 上传文件-文件信息会保存到数据库
 66         /// </summary>
 67         /// <returns></returns>
 68         [HttpPost]
 69         [Route("UploadFileToServer")]
 70         public IHttpActionResult UploadFileToServer()
 71         {
 72             //验证是否是 multipart/form-data
 73             if (!Request.Content.IsMimeMultipartContent("form-data"))
 74             {
 75                 throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
 76             }
 77
 78             List<AttachmentDTO> list = new List<AttachmentDTO>();
 79             string devId = "";
 80             for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
 81             {
 82                 HttpPostedFile file = HttpContext.Current.Request.Files[i];
 83
 84                 string sFileName = "ST_M_" + Guid.NewGuid() + System.IO.Path.GetExtension(file.FileName);
 85                 string path = AppDomain.CurrentDomain.BaseDirectory + ServerUploadFolder + "/";
 86
 87                 file.SaveAs(Path.Combine(path, sFileName));
 88
 89                 AttachmentDTO entity = new AttachmentDTO();
 90                 devId = HttpContext.Current.Request["DevId"].ToString();
 91                 string ForeignID = HttpContext.Current.Request["ForeignID"].ToString();
 92
 93                 entity.ForeignID = ForeignID;
 94                 entity.FileName = sFileName;
 95                 entity.FilePath = ServerUploadFolder;
 96                 entity.UploadTime = DateTime.Now;
 97                 entity.ContentType = file.ContentType;
 98                 entity.FileSize = file.ContentLength;
 99                 entity.CreatedTime = DateTime.Now;
100
101                 list.Add(entity);
102             }
103
104             MobileBL.GetInstance().InsertAttachment(devId, list);
105             return Ok(true);
106         }
107         #endregion
108     }
109     public class RenamingMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
110     {
111         public string Root { get; set; }
112         //public Func<FileUpload.PostedFile, string> OnGetLocalFileName { get; set; }
113
114         public RenamingMultipartFormDataStreamProvider(string root)
115             : base(root)
116         {
117             Root = root;
118         }
119
120         public override string GetLocalFileName(HttpContentHeaders headers)
121         {
122             string filePath = headers.ContentDisposition.FileName;
123
124             // Multipart requests with the file name seem to always include quotes.
125             if (filePath.StartsWith(@"""") && filePath.EndsWith(@""""))
126                 filePath = filePath.Substring(1, filePath.Length - 2);
127
128             var filename = Path.GetFileName(filePath);
129             var extension = Path.GetExtension(filePath);
130             var contentType = headers.ContentType.MediaType;
131
132             return "ST_M_" + Guid.NewGuid() + extension;
133         }
134
135     }

Android Studiod代码下载:点击下载

参考:http://www.cnblogs.com/fly100/articles/3492525.html

时间: 2024-08-05 08:27:23

Android实现表单提交,webapi接收的相关文章

基于Http原理实现Android的图片上传和表单提交

版权声明:本文由张坤  原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/794875001483009140 来源:腾云阁 https://www.qcloud.com/community 现在服务器主要是Web居多,客户端一般通过http上传文件到web服务器,最开始的设想很简单,直接将图片转化为字节流,写入到http的outstream,随后发送出去即可. 但当这种方法出现问题,服务器根据文件名这个表单中的字段来判定

java android布局里的控件值 反射绑定给实体类,实体类绑定给控件,表单提交绑定很有用

注意了:根据实际情况,添加实体里字段的类型,控件类型的判断才可使用.这里控件只有TextView EditText 实体类字段只有String int类型,带值的控件添加tag ,值和实体类的字段值一致 package ice.ui.service; import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import jav

Android Volley解析(二)之表单提交篇

上一篇文章中,讲了 Volley 的 get 和 post 请求,并且对 volley 的基本使用和基本分析做了讲解,而这篇 blog 将讲解用 volley 实现表单的提交,在看这篇文章之前,如果对 Volley 基本知识不够了解的朋友,可以移驾前往Android Volley解析(一)之GET.POST请求篇 表单提交的数据格式 要实现表单的提交,就要知道表单提交的数据格式是怎么样,这里我从某知名网站抓了一条数据,先来分析别人提交表单的数据格式. 数据包: Connection: keep-

asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法

MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交 (2)特殊方式提交 2.控制器处理表单数据的四种方法 方法1:使用传统的Request请求数据 方法2:Action参数名与表单元素name值一一对应 方法3:从MVC封装的FormCollection容器中读取 方法4:使用实体作为Action参数传入,前提是提交的表单元素名称与实体属性名称一一对应 控制器源码 using MvcStudy.Models;u

java jersef rest接收html表单提交

在上传的文件的时候,前台的html的表单,通常会写出enctype="multipart/form-data"格式的,很多人这时候会困惑.通过request.getparameter()接收请求的时候,接收的参数都是null,后台接收不到.有人直接通过servelt去获取,有人通过struts去获取.我的解决方案是通过jersef的rest来解决的. 表单提交两次,在submit按钮的时候写了个onclick方法.按钮先提交submit,将文件上传到服务器.再通过ajax方法传到映射的

input from 表单提交 使用 属性 disabled=&amp;quot;disabled&amp;quot; 后台接收不到name=&amp;quot;username&amp;quot;的值

input from 表单提交 使用 属性 disabled="disabled" 后台接收不到name="username"的值

SpringMVC中使用bean来接收form表单提交的参数时的注意点

这是前辈们对于SpringMVC接收表单数据记录下来的总结经验: SpringMVC接收页面表单参数 springmvc请求参数获取的几种方法 下面是我自己在使用时发现的,前辈们没有记录的细节和注意点: 使用bean来接收form表单提交的参数时,pojo中必须含有默认的(即空的)构造函数,同时,需要设置到bean中的变量必须有setter方法. 注:以下代码均为示例代码,非本人实际运行代码,请自行补充. 例如:我有一个bean类是User,具有变量username和password.同时,表单

android post(HTTP设置参数,仿html表单提交)

package com.example.testhttpurlcon; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta

android form表单上传文件

原文地址:http://menuz.iteye.com/blog/1282097 Android程序使用http上传文件 有时,在网络编程过程中需要向服务器上传文件.Multipart/form-data是上传文件的一种方式. Multipart/form-data其实就是浏览器用表单上传文件的方式.最常见的情境是:在写邮件时,向邮件后添加附件,附件通常使用表单添加,也就是用multipart/form-data格式上传到服务器.  Html代码   <form action="/Test