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

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

HttpURLConnection替代apache httpclient上傳文件

2019-11-09 14:10:31
字體:
來源:轉載
供稿:網友
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();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五家渠市| 祁阳县| 靖宇县| 淮南市| 清水河县| 启东市| 青海省| 百色市| 康马县| 蒙城县| 长治县| 福泉市| 西盟| 蛟河市| 神木县| 嘉荫县| 辽宁省| 祁连县| 丹东市| 徐汇区| 时尚| 紫云| 广元市| 山东省| 潞城市| 拉孜县| 陇南市| 浑源县| 汤阴县| 基隆市| 丹江口市| 花莲市| 鄂托克旗| 黎城县| 上思县| 谷城县| 平乡县| 井研县| 清镇市| 霍邱县| 平阴县|