标签:
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
标签:
原文地址:http://www.cnblogs.com/android-blogs/p/5417863.html