国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HttpURLConnection替代apache httpclient上傳文件

2019-11-09 15:09:04
字體:
來源:轉載
供稿:網友
Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead.apache httpclient上傳文件非常方便,通過HttPRequestBase.setEntity(MultipartEntity)File uploadFile = new File(mUploadFilePath);MultipartEntity multipart = new MultipartEntity();multipart.addPart(mFilePartName, new FileBody(uploadFile, uploadFile.getName()));HttpPost post = new HttpPost("url");post.setEntity(multipart);HttpURLConnection中我們也借助MultipartEntity,而在Android 6.0上要使用這個類的話,我們需要在gradle中配置:useLibrary ‘org.apache.http.legacy’ //Android 6.0 release removes support for the Apache HTTP clientFile uploadFile = new File(mUploadFilePath);MultipartEntity multipart = new MultipartEntity();multipart.addPart(mFilePartName, new FileBody(uploadFile, uploadFile.getName()));HttpURLConnection connection = new URL("url").openConnection();connection.setRequestMethod("POST");connection.setDoOutput(true);Header type = multipart.getContentType();if (type != null) { connection.addRequestProperty(type.getName(), type.getValue());}Header encoding = multipart.getContentEncoding();if (encoding != null) { connection.addRequestProperty(encoding.getName(), encoding.getValue());}if (multipart.isChunked() || multipart.getContentLength() < 0) { connection.setChunkedStreamingMode(0);} else if (multipart.getContentLength() <= 8192) { // Buffer short, fixed-length request bodies. This costs memory, but permits the request // to be transparently retried if there is a connection failure. connection.addRequestProperty("Content-Length", Long.toString(multipart.getContentLength()));} else { connection.setFixedLengthStreamingMode((int) multipart.getContentLength());}OutputStream out = connection.getOutputStream();multipart.writeTo(out);out.close();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太原市| 乌拉特中旗| 密山市| 古田县| 常州市| 定南县| 黄浦区| 漳平市| 周宁县| 长沙市| 高邮市| 广汉市| 历史| 柘城县| 秦安县| 临漳县| 辛集市| 黑山县| 周宁县| 临澧县| 新乡市| 长春市| 三亚市| 江西省| 安岳县| 吴桥县| 高青县| 瑞丽市| 兴隆县| 茶陵县| 虎林市| 长泰县| 盐亭县| 万安县| 奉贤区| 环江| 濮阳市| 腾冲县| 德惠市| 南京市| 临江市|