* 带参数上传文件,可以单个也可以多个
*
* @param strURL
* @param params
* @param files
* @return
* @throws IOException
* @author M.J
*/
public static String postFiles(String strURL, List<String[]> params, ArrayList<File> files) throws IOException {
HttpClient httpclient = getNewHttpClient();
try {
HttpPost httppost = new HttpPost(strURL);
httppost.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 15000);
httppost.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 15000);
MultipartEntity reqEntity = new MultipartEntity();
for (int i = 0; i < files.size(); i++) {
File file = files.get(i);
if (file != null && file.exists()) {
FileBody bin = new FileBody(file);
reqEntity.addPart(String.valueOf(i + 1), bin);
}
long fileLength = file.length();
float length = (float) (fileLength / 1024.0 / 1024.0);
Log.i("NetUtil", "第" + i + "张图片大小为:" + length + "kb");
}
if (params != null) {
for (String[] param : params) {
Log.i("xxxx", " param[0] = " + param[0]);
Log.i("xxxx", " param[1] = " + param[1]);
reqEntity.addPart(param[0], new StringBody(param[1], Charset.forName("UTF-8")));
}
}
httppost.setEntity(reqEntity);
if (httppost.getURI().getRawPath() != null) {
Log.i("xxx", "getRawPath() ====" + httppost.getURI().getRawPath());
}
long end = System.currentTimeMillis();
Log.i("NetUtil", "开始发送请求的时间:" + end + "ms");
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
CookieStore cookieStore = ((DefaultHttpClient) httpclient).getCookieStore();
if (cookieStore != null && cookieStore.getCookies() != null && cookieStore.getCookies().size() > 0) {
PccnApp.getInstance().setCookie(cookieStore);
}
if (resEntity != null) {
InputStream is = resEntity.getContent();
byte[] buffer = new byte[1024];
int count = 0;
StringBuilder resultStr = new StringBuilder();
while ((count = is.read(buffer, 0, 1024)) != -1) {
resultStr.append(new String(buffer, 0, count));
}
is.close();
return resultStr.toString();
}
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
// FITLog.error(ExceptionUtil.getCrashInfo(e));
} catch (ClientProtocolException e) {
e.printStackTrace();
// FITLog.error(ExceptionUtil.getCrashInfo(e));
} finally {
try {
httpclient.getConnectionManager().shutdown();
} catch (Exception ignore) {
}
}
return null;
}
private static synchronized DefaultHttpClient getNewHttpClient() {
DefaultHttpClient httpclient;
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
ConnManagerParams.setTimeout(params, 1000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
httpclient = new DefaultHttpClient(ccm, params);
// HttpClientParams.setCookiePolicy(httpclient.getParams(),
// CookiePolicy.BROWSER_COMPATIBILITY);
CookieStore cookieStore = PccnApp.getInstance().getCookie();
if (cookieStore != null && cookieStore.getCookies() != null && cookieStore.getCookies().size() > 0) {
httpclient.setCookieStore(cookieStore);
}
} catch (Exception e) {
httpclient = new DefaultHttpClient();
// HttpClientParams.setCookiePolicy(httpclient.getParams(),
// CookiePolicy.BROWSER_COMPATIBILITY);
CookieStore cookieStore = PccnApp.getInstance().getCookie();
if (cookieStore != null && cookieStore.getCookies() != null && cookieStore.getCookies().size() > 0) {
httpclient.setCookieStore(cookieStore);
}
}
return httpclient;
}