1
0
mirror of synced 2025-12-14 10:58:07 +08:00

Compare commits

..

22 Commits

Author SHA1 Message Date
Daniel Qian
42418a2d0d upgrade to version 1.1.8 2015-07-17 15:30:20 +08:00
Daniel Qian
8d95028658 fix: IDEA 导入的时候总是会将编译level变成5.0的问题 2015-07-13 17:28:27 +08:00
Daniel Qian
d3d61d753e #165 #167 2015-07-13 17:25:46 +08:00
Daniel Qian
4bee1ba2eb #165 MediaDownloadRequestExecutor出错后就没有办法下载了 2015-07-13 12:32:00 +08:00
Daniel Qian
f30211ce98 upgrade version 1.1.8-SNAPSHOT 2015-07-10 14:03:37 +08:00
Daniel Qian
6239abcbcc update 2015-07-10 13:59:28 +08:00
Daniel Qian
1ade007250 upgrade to 1.1.7 2015-07-10 13:57:40 +08:00
Daniel Qian
ee2cf21efc fix: issue #163 2015-07-02 09:40:12 +08:00
Daniel Qian
d707140d06 upgrade to 1.1.7-SNAPSHOT 2015-06-26 16:40:07 +08:00
Daniel Qian
0491e23456 upgrade to 1.1.6 2015-06-26 16:38:08 +08:00
Daniel Qian
4da9ea5dd5 Merge pull request #155 from huansinho/master
临时文件目录配置
2015-06-15 13:04:50 +08:00
huansinho
f107bd2f44 增加临时文件目录配置
在下载多媒体文件下载时可以指定临时文件目录。
2015-06-14 02:37:31 +08:00
huansinho
50949b1ff5 增加忽略eclipse本地配置文件目录 2015-06-14 01:16:20 +08:00
Daniel Qian
bb22974d65 Merge pull request #151 from darknesstm/develop
统一使用getHttpClient
2015-06-03 15:51:22 +08:00
darknesstm
622094bcdf 统一使用getHttpClient,使得可以复用httpClient 2015-06-03 15:39:06 +08:00
Daniel Qian
bd3fb69bef update to 1.1.6-SNAPSHOT 2015-05-27 09:18:06 +08:00
Daniel Qian
f0432e6930 Merge branch 'develop' 2015-05-27 09:16:23 +08:00
Daniel Qian
6f3dfc7740 Merge branch 'develop' 2015-04-16 14:18:54 +08:00
Daniel Qian
e84ca8dab9 Merge branch 'develop' 2015-03-23 11:09:32 +08:00
Daniel Qian
eccbf08809 Merge branch 'develop'
Conflicts:
	README.md
2015-03-23 11:07:58 +08:00
Daniel Qian
b2179faf3e fix README 2015-02-26 15:10:33 +08:00
Daniel Qian
0cd6033205 Merge branch 'develop' 2015-02-25 15:33:17 +08:00
18 changed files with 205 additions and 101 deletions

45
.gitignore vendored
View File

@@ -1,22 +1,23 @@
*.class
test-output
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
target
.project
.classpath
sw-pom.xml
*.iml
test-config.xml
.idea
*.class
test-output
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
target
.project
.classpath
.settings
sw-pom.xml
*.iml
test-config.xml
.idea

View File

@@ -17,7 +17,7 @@ weixin-java-tools
<dependency>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>1.1.5</version>
<version>1.1.8</version>
</dependency>
```
@@ -27,7 +27,7 @@ weixin-java-tools
<dependency>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>1.1.5</version>
<version>1.1.8</version>
</dependency>
```

12
pom.xml
View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.8</version>
<packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description>
@@ -225,6 +225,16 @@
<goals>deploy</goals>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.8</version>
</parent>
<artifactId>weixin-java-common</artifactId>

View File

@@ -7,18 +7,25 @@ import java.io.InputStream;
public class FileUtils {
/**
* 创建临时文件
* @param inputStream
* @param name 文件名
* @param ext 扩展名
* @param tmpDirFile 临时文件夹目录
* @return
* @throws IOException
*/
public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException {
public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException {
FileOutputStream fos = null;
try {
File tmpFile = File.createTempFile(name, '.' + ext);
File tmpFile;
if (tmpDirFile == null) {
tmpFile = File.createTempFile(name, '.' + ext);
} else {
tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile);
}
tmpFile.deleteOnExit();
fos = new FileOutputStream(tmpFile);
int read = 0;
@@ -43,5 +50,17 @@ public class FileUtils {
}
}
}
/**
* 创建临时文件
* @param inputStream
* @param name 文件名
* @param ext 扩展名
* @return
* @throws IOException
*/
public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException {
return createTmpFile(inputStream, name, ext, null);
}
}

View File

@@ -25,6 +25,18 @@ import java.util.regex.Pattern;
*
*/
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
private File tmpDirFile;
public MediaDownloadRequestExecutor() {
super();
}
public MediaDownloadRequestExecutor(File tmpDirFile) {
super();
this.tmpDirFile = tmpDirFile;
}
@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
@@ -41,26 +53,29 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
httpGet.setConfig(config);
}
CloseableHttpResponse response = httpclient.execute(httpGet);
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 下载媒体文件出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 下载媒体文件出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
}
}
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
// 视频文件不支持下载
String fileName = getFileName(response);
if (StringUtils.isBlank(fileName)) {
return null;
}
String[] name_ext = fileName.split("\\.");
File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1], tmpDirFile);
return localFile;
}
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
// 视频文件不支持下载
String fileName = getFileName(response);
if (StringUtils.isBlank(fileName)) {
return null;
}
String[] name_ext = fileName.split("\\.");
File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1]);
return localFile;
}
protected String getFileName(CloseableHttpResponse response) {

View File

@@ -39,13 +39,14 @@ public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUpload
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
}
CloseableHttpResponse response = httpclient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMediaUploadResult.fromJson(responseContent);
}
return WxMediaUploadResult.fromJson(responseContent);
}
}

View File

@@ -33,13 +33,14 @@ public class SimpleGetRequestExecutor implements RequestExecutor<String, String>
httpGet.setConfig(config);
}
CloseableHttpResponse response = httpclient.execute(httpGet);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
}
return responseContent;
}
}

View File

@@ -40,13 +40,14 @@ public class SimplePostRequestExecutor implements RequestExecutor<String, String
httpPost.setEntity(entity);
}
CloseableHttpResponse response = httpclient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
}
return responseContent;
}
}

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.8</version>
</parent>
<artifactId>weixin-java-cp</artifactId>

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.cp.api;
import java.io.File;
import me.chanjar.weixin.common.bean.WxAccessToken;
/**
@@ -58,5 +60,7 @@ public interface WxCpConfigStorage {
public String getHttp_proxy_username();
public String getHttp_proxy_password();
public File getTmpDirFile();
}

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.cp.api;
import java.io.File;
import me.chanjar.weixin.common.bean.WxAccessToken;
/**
@@ -28,6 +30,8 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
protected volatile String jsapiTicket;
protected volatile long jsapiTicketExpiresTime;
protected volatile File tmpDirFile;
public String getAccessToken() {
return this.accessToken;
}
@@ -189,7 +193,16 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
", http_proxy_password='" + http_proxy_password + '\'' +
", jsapiTicket='" + jsapiTicket + '\'' +
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
", tmpDirFile='" + tmpDirFile + '\'' +
'}';
}
public File getTmpDirFile() {
return tmpDirFile;
}
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
}

View File

@@ -78,6 +78,11 @@ public class WxCpServiceImpl implements WxCpService {
protected WxSessionManager sessionManager = new StandardSessionManager();
/**
* 临时文件目录
*/
protected File tmpDirFile;
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
try {
return SHA1.gen(wxCpConfigStorage.getToken(), timestamp, nonce, data).equals(msgSignature);
@@ -112,8 +117,10 @@ public class WxCpServiceImpl implements WxCpService {
httpGet.setConfig(config);
}
CloseableHttpClient httpclient = getHttpclient();
CloseableHttpResponse response = httpclient.execute(httpGet);
String resultContent = new BasicResponseHandler().handleResponse(response);
String resultContent = null;
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
resultContent = new BasicResponseHandler().handleResponse(response);
}
WxError error = WxError.fromJson(resultContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
@@ -236,7 +243,8 @@ public class WxCpServiceImpl implements WxCpService {
public File mediaDownload(String media_id) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
return execute(new MediaDownloadRequestExecutor(), url, "media_id=" + media_id);
return execute(new MediaDownloadRequestExecutor(wxCpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id);
}
@@ -638,6 +646,14 @@ public class WxCpServiceImpl implements WxCpService {
public void setSessionManager(WxSessionManager sessionManager) {
this.sessionManager = sessionManager;
}
public File getTmpDirFile() {
return tmpDirFile;
}
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
public static void main(String[] args) {
Float a = 3.1f;

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.8</version>
</parent>
<artifactId>weixin-java-mp</artifactId>
<name>WeiXin Java Tools - MP</name>

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api;
import java.io.File;
import me.chanjar.weixin.common.bean.WxAccessToken;
/**
@@ -68,7 +70,8 @@ public interface WxMpConfigStorage {
public String getHttp_proxy_username();
public String getHttp_proxy_password();
public File getTmpDirFile();
}

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api;
import java.io.File;
import me.chanjar.weixin.common.bean.WxAccessToken;
/**
@@ -11,8 +13,8 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
protected volatile String appId;
protected volatile String secret;
protected volatile String partnerId;
protected volatile String partnerKey;
protected volatile String partnerId;
protected volatile String partnerKey;
protected volatile String token;
protected volatile String accessToken;
protected volatile String aesKey;
@@ -28,6 +30,11 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
protected volatile String jsapiTicket;
protected volatile long jsapiTicketExpiresTime;
/**
* 临时文件目录
*/
protected volatile File tmpDirFile;
public String getAccessToken() {
return this.accessToken;
}
@@ -181,24 +188,35 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
", http_proxy_password='" + http_proxy_password + '\'' +
", jsapiTicket='" + jsapiTicket + '\'' +
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
", tmpDirFile='" + tmpDirFile + '\'' +
'}';
}
@Override
public String getPartnerId() {
return partnerId;
}
@Override
public String getPartnerId() {
return partnerId;
}
public void setPartnerId(String partnerId) {
this.partnerId = partnerId;
}
public void setPartnerId(String partnerId) {
this.partnerId = partnerId;
}
@Override
public String getPartnerKey() {
return partnerKey;
}
@Override
public String getPartnerKey() {
return partnerKey;
}
public void setPartnerKey(String partnerKey) {
this.partnerKey = partnerKey;
}
@Override
public File getTmpDirFile() {
return this.tmpDirFile;
}
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
public void setPartnerKey(String partnerKey) {
this.partnerKey = partnerKey;
}
}

View File

@@ -7,6 +7,7 @@ import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.thoughtworks.xstream.XStream;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
@@ -27,6 +28,7 @@ import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
@@ -108,8 +110,7 @@ public class WxMpServiceImpl implements WxMpService {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
CloseableHttpClient httpclient = getHttpclient();
CloseableHttpResponse response = httpclient.execute(httpGet);
CloseableHttpResponse response = getHttpclient().execute(httpGet);
String resultContent = new BasicResponseHandler().handleResponse(response);
WxError error = WxError.fromJson(resultContent);
if (error.getErrorCode() != 0) {
@@ -214,7 +215,7 @@ public class WxMpServiceImpl implements WxMpService {
public File mediaDownload(String media_id) throws WxErrorException {
String url = "http://file.api.weixin.qq.com/cgi-bin/media/get";
return execute(new MediaDownloadRequestExecutor(), url, "media_id=" + media_id);
return execute(new MediaDownloadRequestExecutor(wxMpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id);
}
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
@@ -672,7 +673,7 @@ public class WxMpServiceImpl implements WxMpService {
StringEntity entity = new StringEntity(xml, Consts.UTF_8);
httpPost.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
CloseableHttpResponse response = getHttpclient().execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxMpPrepayIdResult.class);

View File

@@ -47,20 +47,21 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
httpGet.setConfig(config);
}
CloseableHttpResponse response = httpclient.execute(httpGet);
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
}
}
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
return localFile;
}
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
return localFile;
}
}