Skip to content

Commit b5012ad

Browse files
authored
Merge pull request #31 from xdx54321/master
更新到1.1.8
2 parents a9a7a3d + 592bf0d commit b5012ad

File tree

4 files changed

+138
-21
lines changed

4 files changed

+138
-21
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jiguang-common</artifactId>
6-
<version>1.1.8-SNAPSHOT</version>
6+
<version>1.1.8</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jiguang-java-client-common</url>
99
<name>Jiguang Client Common Dependencies</name>
@@ -34,7 +34,7 @@
3434
<url>https://github.com/jpush/jiguang-java-client-common</url>
3535
<connection>scm:git:[email protected]:jpush/jiguang-java-client-common.git</connection>
3636
<developerConnection>scm:git:[email protected]:jpush/jiguang-java-client-common.git</developerConnection>
37-
<tag>jiguang-common-1.1.4</tag>
37+
<tag>jiguang-common-1.1.8</tag>
3838
</scm>
3939

4040
<dependencies>

src/main/java/cn/jiguang/common/ClientConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class ClientConfig extends HashMap<String, Object> {
6161
public static final String GROUP_PUSH_PATH = "group.push.path";
6262
public static final Object GROUP_PUSH_PATH_SCHEMA = String.class;
6363

64+
public static final String V3_FILES_PATH = "jpush.v3.files.path";
65+
public static final Object V3_FILES_PATH_SCHEMA = String.class;
66+
6467
public static final String SSL_VERSION = "ssl.version";
6568
public static final Object SSL_VERSION_SCHEMA = String.class;
6669
public static final String DEFAULT_SSL_VERSION = "TLS";
@@ -144,6 +147,8 @@ private ClientConfig() {
144147
this.put(SCHEDULE_HOST_NAME, "https://api.jpush.cn");
145148
this.put(SCHEDULE_PATH, "/v3/schedules");
146149

150+
this.put(V3_FILES_PATH, "/v3/files");
151+
147152
this.put(SSL_VERSION, DEFAULT_SSL_VERSION);
148153
this.put(MAX_RETRY_TIMES, DEFULT_MAX_RETRY_TIMES);
149154
this.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT);

src/main/java/cn/jiguang/common/connection/IHttpClient.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ public interface IHttpClient {
1313
public static final String CHARSET = "UTF-8";
1414
public static final String CONTENT_TYPE_JSON = "application/json";
1515
public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
16-
16+
1717
public static final String RATE_LIMIT_QUOTA = "X-Rate-Limit-Limit";
1818
public static final String RATE_LIMIT_Remaining = "X-Rate-Limit-Remaining";
1919
public static final String RATE_LIMIT_Reset = "X-Rate-Limit-Reset";
2020
public static final String JPUSH_USER_AGENT = "JPush-API-Java-Client";
21-
21+
2222
public static final int RESPONSE_OK = 200;
23-
23+
2424
public enum RequestMethod {
25-
GET,
25+
GET,
2626
POST,
2727
PUT,
2828
DELETE
2929
}
30-
30+
3131
public static final String IO_ERROR_MESSAGE = "Connection IO error. \n"
3232
+ "Can not connect to JPush Server. "
3333
+ "Please ensure your internet connection is ok. \n"
@@ -49,27 +49,27 @@ public enum RequestMethod {
4949

5050
//设置连接超时时间
5151
public static final int DEFAULT_CONNECTION_TIMEOUT = (5 * 1000); // milliseconds
52-
52+
5353
//设置读取超时时间
5454
public static final int DEFAULT_READ_TIMEOUT = (30 * 1000); // milliseconds
55-
55+
5656
public static final int DEFAULT_MAX_RETRY_TIMES = 3;
5757

58-
public ResponseWrapper sendGet(String url)
58+
public ResponseWrapper sendGet(String url)
5959
throws APIConnectionException, APIRequestException;
6060

6161
public ResponseWrapper sendGet(String url, String content)
6262
throws APIConnectionException, APIRequestException;
63-
64-
public ResponseWrapper sendDelete(String url)
63+
64+
public ResponseWrapper sendDelete(String url)
6565
throws APIConnectionException, APIRequestException;
6666

6767
public ResponseWrapper sendDelete(String url, String content)
6868
throws APIConnectionException, APIRequestException;
69-
70-
public ResponseWrapper sendPost(String url, String content)
69+
70+
public ResponseWrapper sendPost(String url, String content)
7171
throws APIConnectionException, APIRequestException;
72-
72+
7373

7474
public ResponseWrapper sendPut(String url, String content)
7575
throws APIConnectionException, APIRequestException;

src/main/java/cn/jiguang/common/connection/NativeHttpClient.java

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11+
import javax.activation.MimetypesFileTypeMap;
1112
import javax.net.ssl.*;
12-
import java.io.IOException;
13-
import java.io.InputStream;
14-
import java.io.InputStreamReader;
15-
import java.io.OutputStream;
13+
import java.io.*;
1614
import java.net.*;
1715
import java.security.cert.CertificateException;
1816
import java.security.cert.X509Certificate;
1917
import java.text.MessageFormat;
18+
import java.util.Iterator;
19+
import java.util.Map;
2020

2121
/**
2222
* The implementation has no connection pool mechanism, used origin java connection.
@@ -283,7 +283,7 @@ private ResponseWrapper _doRequest(String url, String content,
283283
}
284284

285285
protected void initSSL(String sslVer) {
286-
TrustManager[] tmCerts = new javax.net.ssl.TrustManager[1];
286+
TrustManager[] tmCerts = new TrustManager[1];
287287
tmCerts[0] = new SimpleTrustManager();
288288
try {
289289
SSLContext sslContext = SSLContext.getInstance(sslVer);
@@ -297,6 +297,118 @@ protected void initSSL(String sslVer) {
297297
}
298298
}
299299

300+
public String formUpload(String urlStr, Map<String, String> textMap,
301+
Map<String, String> fileMap, String contentType) {
302+
String res = "";
303+
HttpURLConnection conn = null;
304+
// boundary就是request头和上传文件内容的分隔符
305+
String BOUNDARY = "---------------------------" + System.currentTimeMillis();
306+
try {
307+
URL url = new URL(urlStr);
308+
conn = (HttpURLConnection) url.openConnection();
309+
conn.setConnectTimeout(5000);
310+
conn.setReadTimeout(30000);
311+
conn.setDoOutput(true);
312+
conn.setDoInput(true);
313+
conn.setUseCaches(false);
314+
conn.setRequestMethod("POST");
315+
conn.setRequestProperty("Connection", "Keep-Alive");
316+
conn.setRequestProperty("Authorization", _authCode);
317+
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
318+
OutputStream out = new DataOutputStream(conn.getOutputStream());
319+
// text
320+
if (textMap != null) {
321+
StringBuffer strBuf = new StringBuffer();
322+
Iterator iter = textMap.entrySet().iterator();
323+
while (iter.hasNext()) {
324+
Map.Entry entry = (Map.Entry) iter.next();
325+
String inputName = (String) entry.getKey();
326+
String inputValue = (String) entry.getValue();
327+
if (inputValue == null) {
328+
continue;
329+
}
330+
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
331+
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
332+
strBuf.append(inputValue);
333+
}
334+
out.write(strBuf.toString().getBytes());
335+
}
336+
// file
337+
if (fileMap != null) {
338+
Iterator iter = fileMap.entrySet().iterator();
339+
while (iter.hasNext()) {
340+
Map.Entry entry = (Map.Entry) iter.next();
341+
String inputName = (String) entry.getKey();
342+
String inputValue = (String) entry.getValue();
343+
if (inputValue == null) {
344+
continue;
345+
}
346+
File file = new File(inputValue);
347+
String filename = file.getName();
348+
349+
//没有传入文件类型,同时根据文件获取不到类型,默认采用application/octet-stream
350+
contentType = new MimetypesFileTypeMap().getContentType(file);
351+
//contentType非空采用filename匹配默认的图片类型
352+
if (!"".equals(contentType)) {
353+
if (filename.endsWith(".png")) {
354+
contentType = "image/png";
355+
} else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".jpe")) {
356+
contentType = "image/jpeg";
357+
} else if (filename.endsWith(".gif")) {
358+
contentType = "image/gif";
359+
} else if (filename.endsWith(".ico")) {
360+
contentType = "image/image/x-icon";
361+
}
362+
}
363+
if (contentType == null || "".equals(contentType)) {
364+
contentType = "application/octet-stream";
365+
}
366+
StringBuffer strBuf = new StringBuffer();
367+
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
368+
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");
369+
strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
370+
out.write(strBuf.toString().getBytes());
371+
DataInputStream in = new DataInputStream(new FileInputStream(file));
372+
int bytes = 0;
373+
byte[] bufferOut = new byte[1024];
374+
while ((bytes = in.read(bufferOut)) != -1) {
375+
out.write(bufferOut, 0, bytes);
376+
}
377+
in.close();
378+
}
379+
}
380+
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
381+
out.write(endData);
382+
out.flush();
383+
out.close();
384+
// 读取返回数据
385+
StringBuffer strBuf = new StringBuffer();
386+
387+
InputStream is = null;
388+
int responseCode = conn.getResponseCode();
389+
BufferedReader reader;
390+
if (responseCode == 200) {
391+
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
392+
} else {
393+
reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
394+
}
395+
String line = null;
396+
while ((line = reader.readLine()) != null) {
397+
strBuf.append(line).append("\n");
398+
}
399+
res = strBuf.toString();
400+
reader.close();
401+
reader = null;
402+
} catch (Exception e) {
403+
LOG.error("formUpload error", e);
404+
} finally {
405+
if (conn != null) {
406+
conn.disconnect();
407+
conn = null;
408+
}
409+
}
410+
return res;
411+
}
300412

301413
private static class SimpleHostnameVerifier implements HostnameVerifier {
302414

@@ -323,7 +435,7 @@ public X509Certificate[] getAcceptedIssuers() {
323435
}
324436
}
325437

326-
public static class SimpleProxyAuthenticator extends java.net.Authenticator {
438+
public static class SimpleProxyAuthenticator extends Authenticator {
327439
private String username;
328440
private String password;
329441

0 commit comments

Comments
 (0)