国产探花免费观看_亚洲丰满少妇自慰呻吟_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();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卓资县| 灵丘县| 陕西省| 收藏| 定州市| 民权县| 株洲市| 广宗县| 德钦县| 河西区| 甘德县| 平遥县| 宾川县| 潞西市| 周宁县| 县级市| 隆林| 延边| 扶沟县| 邢台市| 平顶山市| 逊克县| 册亨县| 霍城县| 溧水县| 涟水县| 固镇县| 桓台县| 彭阳县| 静宁县| 延长县| 和静县| 兴文县| 仙居县| 大同县| 巴彦淖尔市| 岑溪市| 潜江市| 定日县| 永宁县| 郓城县|