Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
chanjarster committed Jun 26, 2015
2 parents f0432e6 + 0491e23 commit 825393d
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 39 deletions.
45 changes: 23 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.6</version>
</dependency>
```

Expand All @@ -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.6</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.6</version>
<packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description>
Expand Down
2 changes: 1 addition & 1 deletion weixin-java-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</parent>

<artifactId>weixin-java-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

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;
Expand All @@ -43,5 +50,17 @@ public static File createTmpFile(InputStream inputStream, String name, String ex
}
}
}

/**
* 创建临时文件
* @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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
*
*/
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 {
Expand Down Expand Up @@ -59,7 +71,7 @@ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String u
return null;
}
String[] name_ext = fileName.split("\\.");
File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1]);
File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1], tmpDirFile);
return localFile;
}

Expand Down
2 changes: 1 addition & 1 deletion weixin-java-cp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</parent>

<artifactId>weixin-java-cp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.chanjar.weixin.cp.api;

import java.io.File;

import me.chanjar.weixin.common.bean.WxAccessToken;

/**
Expand Down Expand Up @@ -58,5 +60,7 @@ public interface WxCpConfigStorage {
public String getHttp_proxy_username();

public String getHttp_proxy_password();

public File getTmpDirFile();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.chanjar.weixin.cp.api;

import java.io.File;

import me.chanjar.weixin.common.bean.WxAccessToken;

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -189,7 +193,16 @@ public String toString() {
", 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -236,7 +241,8 @@ public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErr

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);
}


Expand Down Expand Up @@ -638,6 +644,14 @@ public WxSession getSession(String id, boolean create) {
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;
Expand Down
2 changes: 1 addition & 1 deletion weixin-java-mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</parent>
<artifactId>weixin-java-mp</artifactId>
<name>WeiXin Java Tools - MP</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api;

import java.io.File;

import me.chanjar.weixin.common.bean.WxAccessToken;

/**
Expand Down Expand Up @@ -68,7 +70,8 @@ public interface WxMpConfigStorage {

public String getHttp_proxy_username();


public String getHttp_proxy_password();

public File getTmpDirFile();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api;

import java.io.File;

import me.chanjar.weixin.common.bean.WxAccessToken;

/**
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -181,6 +188,7 @@ public String toString() {
", http_proxy_password='" + http_proxy_password + '\'' +
", jsapiTicket='" + jsapiTicket + '\'' +
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
", tmpDirFile='" + tmpDirFile + '\'' +
'}';
}

Expand All @@ -201,4 +209,9 @@ public String getPartnerKey() {
public void setPartnerKey(String partnerKey) {
this.partnerKey = partnerKey;
}

@Override
public File getTmpDirFile() {
return this.getTmpDirFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -27,6 +28,7 @@
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;
Expand Down Expand Up @@ -108,8 +110,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
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) {
Expand Down Expand Up @@ -214,7 +215,7 @@ public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErr

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 {
Expand Down Expand Up @@ -672,7 +673,7 @@ public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double a
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);
Expand Down

0 comments on commit 825393d

Please sign in to comment.