|
|
@@ -1,357 +0,0 @@
|
|
|
-package com.kfzs.duanduan.utils.net;
|
|
|
-
|
|
|
-import android.text.TextUtils;
|
|
|
-import android.util.Log;
|
|
|
-
|
|
|
-import com.kfzs.appstore.utils.string.MD5Utils;
|
|
|
-import com.sheep.jiuyan.samllsheep.BuildConfig;
|
|
|
-
|
|
|
-import java.io.DataOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-
|
|
|
-/**
|
|
|
- * this utils use {@link HttpURLConnection} and send file to new {@link Thread}
|
|
|
- * <br/><li/>must not be use at UI thread
|
|
|
- * <pre>
|
|
|
- * sinlov
|
|
|
- *
|
|
|
- * /\__/\
|
|
|
- * /` '\
|
|
|
- * ≈≈≈ 0 0 ≈≈≈ Hello world!
|
|
|
- * \ -- /
|
|
|
- * / \
|
|
|
- * / \
|
|
|
- * | |
|
|
|
- * \ || || /
|
|
|
- * \_oo__oo_/≡≡≡≡≡≡≡≡o
|
|
|
- *
|
|
|
- * </pre>
|
|
|
- * Created by sinlov on 17/3/30.
|
|
|
- */
|
|
|
-public class UploadHttpURLConnectionUtils {
|
|
|
-
|
|
|
- public static final boolean debug = BuildConfig.KF_DEBUG;
|
|
|
- public static final int SET_DEFAULT_TIME_OUT = 30 * 1000;
|
|
|
- public static final int SET_DEFAULT_PROGRESS_PERCENTAGE = 99;
|
|
|
- public static final String SET_DEFAULT_CHARSET = "utf-8";
|
|
|
- public static final String SET_DEFAULT_REQUEST_METHOD = "POST";
|
|
|
-
|
|
|
- public static final String KEY_DEFAULT_SECURITY = "Authorization";
|
|
|
- public static final String KEY_DEFAULT_FILE_NAME = "asset_name";
|
|
|
- public static final String KEY_DEFAULT_MD5 = "md5_key";
|
|
|
-
|
|
|
- public static final String MSG_DEFAULT_FILE_IS_EMPTY = "you want send file is empty";
|
|
|
- public static final String MSG_DEFAULT_OUT_OF_TIME = "sending error, out of time";
|
|
|
- public static final String MSG_DEFAULT_RESPONSE_IS_EMPTY = "response is empty";
|
|
|
-
|
|
|
- private static final String TAG = "UploadFile";
|
|
|
- private static final String LINE_END = "\r\n";
|
|
|
- private static final String PREFIX = "--";
|
|
|
- private static final String CONTENT_DISPOSITION_START = "Content-Disposition: form-data;";
|
|
|
- private static final String CONTENT_DISPOSITION_FROM_START = " ";
|
|
|
- private static final String CONTENT_DISPOSITION_FROM_MID = "=\"";
|
|
|
- private static final String CONTENT_DISPOSITION_FROM_END = "\";";
|
|
|
- private static final String CONTENT_TYPE_FORM_DATA_DEFAULT = "multipart/form-data";
|
|
|
- private static final String CONTENT_TYPE_OCTET_STREAM_CHARSET = "Content-Type: image/jpeg; charset=";
|
|
|
-
|
|
|
- private final String filenameKey;
|
|
|
- private int newLen = 0;
|
|
|
- private int fileMaxLen;
|
|
|
- private int nowLen;
|
|
|
- private ConcurrentHashMap<String, String> urlParams;
|
|
|
- private ConcurrentHashMap<String, String> fromData;
|
|
|
-
|
|
|
- private String requestMethod = SET_DEFAULT_REQUEST_METHOD;
|
|
|
- private String charset = SET_DEFAULT_CHARSET;
|
|
|
- private String contentType = CONTENT_TYPE_FORM_DATA_DEFAULT;
|
|
|
- private int timeOut = SET_DEFAULT_TIME_OUT;
|
|
|
- private String md5Key = KEY_DEFAULT_MD5;
|
|
|
- private String securityKey = KEY_DEFAULT_SECURITY;
|
|
|
- private int progressPercentage = SET_DEFAULT_PROGRESS_PERCENTAGE;
|
|
|
-
|
|
|
- private String errorMsgFileEmpty = MSG_DEFAULT_FILE_IS_EMPTY;
|
|
|
- private String errorMsgOutOfTime = MSG_DEFAULT_OUT_OF_TIME;
|
|
|
- private String errorMsgResponseEmpty = MSG_DEFAULT_RESPONSE_IS_EMPTY;
|
|
|
-
|
|
|
- private String securityValue;
|
|
|
-
|
|
|
- private String sendingType;
|
|
|
-
|
|
|
- private UploadFileCallBack uploadFileCallBack;
|
|
|
-
|
|
|
- /**
|
|
|
- * default percentage {@link #SET_DEFAULT_PROGRESS_PERCENTAGE}
|
|
|
- *
|
|
|
- * @param progressPercentage percentage
|
|
|
- */
|
|
|
- public void setProgressPercentage(int progressPercentage) {
|
|
|
- this.progressPercentage = progressPercentage;
|
|
|
- }
|
|
|
-
|
|
|
- public void setSecurityValue(String securityValue) {
|
|
|
- this.securityValue = securityValue;
|
|
|
- }
|
|
|
-
|
|
|
- public void setSecurityKey(String securityKey) {
|
|
|
- this.securityKey = securityKey;
|
|
|
- }
|
|
|
-
|
|
|
- public void setErrorMsgFileEmpty(String errorMsgFileEmpty) {
|
|
|
- this.errorMsgFileEmpty = errorMsgFileEmpty;
|
|
|
- }
|
|
|
-
|
|
|
- public void setErrorMsgOutOfTime(String errorMsgOutOfTime) {
|
|
|
- this.errorMsgOutOfTime = errorMsgOutOfTime;
|
|
|
- }
|
|
|
-
|
|
|
- public void setErrorMsgResponseEmpty(String errorMsgResponseEmpty) {
|
|
|
- this.errorMsgResponseEmpty = errorMsgResponseEmpty;
|
|
|
- }
|
|
|
-
|
|
|
- public void setUrlParams(ConcurrentHashMap<String, String> urlParams) {
|
|
|
- this.urlParams = urlParams;
|
|
|
- }
|
|
|
-
|
|
|
- public void setFromData(ConcurrentHashMap<String, String> fromData) {
|
|
|
- this.fromData = fromData;
|
|
|
- }
|
|
|
-
|
|
|
- public void setTimeOut(int timeOut) {
|
|
|
- this.timeOut = timeOut;
|
|
|
- }
|
|
|
-
|
|
|
- public void setRequestMethod(String requestMethod) {
|
|
|
- this.requestMethod = requestMethod;
|
|
|
- }
|
|
|
-
|
|
|
- public void setCharset(String charset) {
|
|
|
- this.charset = charset;
|
|
|
- }
|
|
|
-
|
|
|
- public void setMd5Key(String md5Key) {
|
|
|
- this.md5Key = md5Key;
|
|
|
- }
|
|
|
-
|
|
|
- public void setContentType(String contentType) {
|
|
|
- this.contentType = contentType;
|
|
|
- }
|
|
|
-
|
|
|
- public void setUploadFileCallBack(UploadFileCallBack uploadFileCallBack) {
|
|
|
- this.uploadFileCallBack = uploadFileCallBack;
|
|
|
- }
|
|
|
-
|
|
|
- public int getFileMaxLen() {
|
|
|
- return fileMaxLen;
|
|
|
- }
|
|
|
-
|
|
|
- public void uploadFileByThread(final File file, final String requestURL) {
|
|
|
- Thread uploadThread = new Thread() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- uploadFile(file, requestURL);
|
|
|
- }
|
|
|
- };
|
|
|
- uploadThread.start();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * upload file to services, and return http response code
|
|
|
- * <ol>
|
|
|
- * <li/> if out of time will return {@link HttpURLConnection#HTTP_INTERNAL_ERROR}
|
|
|
- * </ol>
|
|
|
- *
|
|
|
- * @param file for upload file object
|
|
|
- * @param requestURL request full url
|
|
|
- * @return http response
|
|
|
- */
|
|
|
- private int uploadFile(File file, String requestURL) {
|
|
|
- int resCode = HttpURLConnection.HTTP_INTERNAL_ERROR;
|
|
|
- try {
|
|
|
- if (file != null) {
|
|
|
-
|
|
|
- String BOUNDARY = UUID.randomUUID().toString(); // random UUID for boundary
|
|
|
- ConcurrentHashMap<String, String> urlParesData = new ConcurrentHashMap<String, String>();
|
|
|
- if (urlParams != null) {
|
|
|
- urlParesData.putAll(urlParams);
|
|
|
- }
|
|
|
- String md5Value = MD5Utils.md5CheckFileByFile(file, MD5Utils.TYPE_LOWER);
|
|
|
- if (!TextUtils.isEmpty(md5Value)) {
|
|
|
- urlParesData.put(md5Key, md5Value);
|
|
|
- } else {
|
|
|
- if (uploadFileCallBack != null) {
|
|
|
- uploadFileCallBack.responseUploadError(UploadFileCallBack.ERROR_FILE_MD5_HASH_ERROR, "file md5 hash error");
|
|
|
- }
|
|
|
- return UploadFileCallBack.ERROR_FILE_MD5_HASH_ERROR;
|
|
|
- }
|
|
|
- requestURL = matchURL(requestURL, urlParesData);
|
|
|
- // fromMatchData
|
|
|
- ConcurrentHashMap<String, String> fromMatchData = new ConcurrentHashMap<String, String>();
|
|
|
- fromMatchData.put("name", filenameKey);
|
|
|
- fromMatchData.put("filename", file.getName());
|
|
|
- if (fromData != null) {
|
|
|
- fromMatchData.putAll(fromData);
|
|
|
- }
|
|
|
- // httpURLConnection setting
|
|
|
- URL url = new URL(requestURL);
|
|
|
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
- conn.setReadTimeout(timeOut);
|
|
|
- conn.setConnectTimeout(timeOut);
|
|
|
- conn.setDoInput(true); // allow input
|
|
|
- conn.setDoOutput(true); // allow output
|
|
|
- conn.setUseCaches(false); // not allow user catch
|
|
|
- conn.setRequestMethod(requestMethod); // request method
|
|
|
- conn.setRequestProperty("Charset", charset); // set char
|
|
|
- conn.setRequestProperty("connection", "keep-alive");// connection way
|
|
|
- if (!TextUtils.isEmpty(securityValue)) {
|
|
|
- conn.setRequestProperty(securityKey, securityValue);
|
|
|
- }
|
|
|
- conn.setRequestProperty("Content-Type", contentType + ";boundary=" + BOUNDARY);// setting UUID for boundary
|
|
|
- DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
|
|
|
-
|
|
|
- StringBuffer contentAppender = new StringBuffer();
|
|
|
- contentAppender.append(PREFIX);
|
|
|
- contentAppender.append(BOUNDARY);
|
|
|
- contentAppender.append(LINE_END);
|
|
|
-
|
|
|
- // match form data
|
|
|
- matchFormData(contentAppender, fromMatchData);
|
|
|
-
|
|
|
- // match stream head
|
|
|
- contentAppender.append(CONTENT_TYPE_OCTET_STREAM_CHARSET)
|
|
|
- .append(charset)
|
|
|
- .append(LINE_END);
|
|
|
- contentAppender.append(LINE_END);
|
|
|
- // append file stream
|
|
|
- dos.write(contentAppender.toString().getBytes());
|
|
|
- InputStream is = new FileInputStream(file);
|
|
|
- byte[] bytes = new byte[1024];
|
|
|
- fileMaxLen = is.available();
|
|
|
- int len;
|
|
|
- while ((len = is.read(bytes)) != -1) {
|
|
|
- dos.write(bytes, 0, len);
|
|
|
- newLen = newLen + len;
|
|
|
- if (uploadFileCallBack != null) {
|
|
|
- int progress = (newLen * progressPercentage) / fileMaxLen;
|
|
|
- if (progress != nowLen) {
|
|
|
- nowLen = progress;
|
|
|
- }
|
|
|
- uploadFileCallBack.uploadProgress(nowLen);
|
|
|
- }
|
|
|
- }
|
|
|
- is.close();
|
|
|
- dos.write(LINE_END.getBytes());
|
|
|
- byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
|
|
|
- .getBytes();
|
|
|
- dos.write(end_data);
|
|
|
- dos.flush();
|
|
|
- // start get response
|
|
|
- resCode = conn.getResponseCode();
|
|
|
- InputStream input = conn.getInputStream();
|
|
|
- StringBuffer responseAppender = new StringBuffer();
|
|
|
- if (input != null) {
|
|
|
- int ss;
|
|
|
- while ((ss = input.read()) != -1) {
|
|
|
- responseAppender.append((char) ss);
|
|
|
- }
|
|
|
- } else {
|
|
|
- responseAppender.append(errorMsgResponseEmpty);
|
|
|
- }
|
|
|
- if (resCode == 200) {
|
|
|
- if (uploadFileCallBack != null) {
|
|
|
- uploadFileCallBack.responseUploadSuccess(responseAppender.toString());
|
|
|
- }
|
|
|
- } else {
|
|
|
- uploadFileCallBack.responseUploadError(resCode, responseAppender.toString());
|
|
|
- w("request error: " + responseAppender.toString());
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (uploadFileCallBack != null) {
|
|
|
- uploadFileCallBack.responseUploadError(UploadFileCallBack.ERROR_FILE_IS_EMPTY, errorMsgFileEmpty);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- uploadFileCallBack.responseUploadError(UploadFileCallBack.ERROR_OUT_OF_TIME, errorMsgOutOfTime);
|
|
|
- }
|
|
|
- return resCode;
|
|
|
- }
|
|
|
-
|
|
|
- private String matchURL(String head, ConcurrentHashMap<String, String> urlParams) {
|
|
|
- if (null == urlParams) {
|
|
|
- return head;
|
|
|
- }
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- sb.append(head);
|
|
|
- sb.append("?");
|
|
|
- for (Map.Entry<String, String> param : urlParams.entrySet()) {
|
|
|
- sb.append(param.getKey());
|
|
|
- sb.append("=");
|
|
|
- sb.append(param.getValue());
|
|
|
- sb.append("&");
|
|
|
- }
|
|
|
- sb.setLength(sb.length() - 1);
|
|
|
- return sb.toString();
|
|
|
- }
|
|
|
-
|
|
|
- private void matchFormData(StringBuffer strBuffer, ConcurrentHashMap<String, String> formData) {
|
|
|
- strBuffer.append(CONTENT_DISPOSITION_START);
|
|
|
- for (Map.Entry data : formData.entrySet()) {
|
|
|
- strBuffer.append(CONTENT_DISPOSITION_FROM_START);
|
|
|
- strBuffer.append(data.getKey());
|
|
|
- strBuffer.append(CONTENT_DISPOSITION_FROM_MID);
|
|
|
- strBuffer.append(data.getValue());
|
|
|
- strBuffer.append(CONTENT_DISPOSITION_FROM_END);
|
|
|
- }
|
|
|
- strBuffer.append(LINE_END);
|
|
|
- }
|
|
|
-
|
|
|
- private void e(String msg) {
|
|
|
- if (debug) {
|
|
|
- Log.e(TAG, msg);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void e(String msg, Throwable tr) {
|
|
|
- if (debug) {
|
|
|
- Log.e(TAG, msg, tr);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void w(String msg) {
|
|
|
- if (debug) {
|
|
|
- Log.w(TAG, msg);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void w(String msg, Throwable tr) {
|
|
|
- if (debug) {
|
|
|
- Log.w(TAG, msg, tr);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * set services get file key {@link #KEY_DEFAULT_FILE_NAME}
|
|
|
- */
|
|
|
- public UploadHttpURLConnectionUtils() {
|
|
|
- this.filenameKey = KEY_DEFAULT_FILE_NAME;
|
|
|
-
|
|
|
- this.sendingType = "Content-Type: image/jpeg; charset=";
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * filenameKey is Service get file Key
|
|
|
- *
|
|
|
- * @param filenameKey {@link String}
|
|
|
- */
|
|
|
- public UploadHttpURLConnectionUtils(String filenameKey) {
|
|
|
- this.filenameKey = filenameKey;
|
|
|
- }
|
|
|
-}
|