国产探花免费观看_亚洲丰满少妇自慰呻吟_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();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄迈县| 广昌县| 宝鸡市| 那曲县| 屏山县| 合水县| 舞钢市| 浮梁县| 青龙| 富裕县| 余江县| 蓬安县| 兴业县| 黎川县| 平山县| 顺昌县| 盐池县| 定襄县| 洛扎县| 濉溪县| 英吉沙县| 稻城县| 徐水县| 兴化市| 汶上县| 吴忠市| 吉木乃县| 潮安县| 泽州县| 玛沁县| 诏安县| 星座| 博乐市| 西乡县| 贵阳市| 健康| 苏尼特左旗| 武陟县| 汕头市| 木里| 彭阳县|