#362 修复okhttp实现的qrcode二维码图片下载代码

This commit is contained in:
Binary Wang 2017-11-03 12:04:33 +08:00
parent 8a587174a1
commit 29149424ac
4 changed files with 38 additions and 38 deletions

View File

@ -1,52 +1,37 @@
package me.chanjar.weixin.common.util.fs; package me.chanjar.weixin.common.util.fs;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
public class FileUtils { public class FileUtils {
/** /**
* 创建临时文件 * 创建临时文件
* *
* @param inputStream * @param inputStream 输入文件流
* @param name 文件名 * @param name 文件名
* @param ext 扩展名 * @param ext 扩展名
* @param tmpDirFile 临时文件夹目录 * @param tmpDirFile 临时文件夹目录
*/ */
public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException { public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException {
File tmpFile; File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile);
if (tmpDirFile == null) {
tmpFile = File.createTempFile(name, '.' + ext);
} else {
tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile);
}
tmpFile.deleteOnExit(); resultFile.deleteOnExit();
org.apache.commons.io.FileUtils.copyInputStreamToFile(inputStream, resultFile);
try (FileOutputStream fos = new FileOutputStream(tmpFile)) { return resultFile;
int read = 0;
byte[] bytes = new byte[1024 * 100];
while ((read = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
fos.flush();
return tmpFile;
}
} }
/** /**
* 创建临时文件 * 创建临时文件
* *
* @param inputStream * @param inputStream 输入文件流
* @param name 文件名 * @param name 文件名
* @param ext 扩展名 * @param ext 扩展名
*/ */
public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException { public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException {
return createTmpFile(inputStream, name, ext, null); return createTmpFile(inputStream, name, ext, Files.createTempDirectory("weixin-java-tools-temp").toFile());
} }
} }

View File

@ -7,21 +7,22 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor; import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
import okhttp3.OkHttpClient;
import okhttp3.*; import okhttp3.Request;
import okio.BufferedSink; import okhttp3.Response;
import okio.Okio;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID; import java.util.UUID;
/** /**
* Created by ecoolper on 2017/5/5. *
* @author ecoolper
* @date 2017/5/5
*/ */
public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<OkHttpClient, OkHttpProxyInfo> { public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
@ -31,9 +32,18 @@ public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<OkHttpCli
} }
@Override @Override
public File execute(String uri, WxMpQrCodeTicket data) throws WxErrorException, IOException { public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
logger.debug("OkhttpQrCodeRequestExecutor is running"); logger.debug("OkhttpQrCodeRequestExecutor is running");
//得到httpClient
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?")
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
OkHttpClient client = requestHttp.getRequestHttpClient(); OkHttpClient client = requestHttp.getRequestHttpClient();
Request request = new Request.Builder().url(uri).get().build(); Request request = new Request.Builder().url(uri).get().build();
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
@ -42,12 +52,10 @@ public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<OkHttpCli
String responseContent = response.body().string(); String responseContent = response.body().string();
throw new WxErrorException(WxError.fromJson(responseContent)); throw new WxErrorException(WxError.fromJson(responseContent));
} }
File temp = File.createTempFile(UUID.randomUUID().toString(), ".png");
try (BufferedSink sink = Okio.buffer(Okio.sink(temp))) {
sink.writeAll(response.body().source());
}
temp.deleteOnExit();
return temp; try (InputStream inputStream = response.body().byteStream()) {
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
}
} }
} }

View File

@ -5,11 +5,13 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule; import me.chanjar.weixin.mp.api.test.ApiTestModule;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.testng.*; import org.testng.*;
import org.testng.annotations.*; import org.testng.annotations.*;
import java.io.File; import java.io.File;
import java.io.IOException;
/** /**
* 测试用户相关的接口 * 测试用户相关的接口
@ -65,6 +67,12 @@ public class WxMpQrcodeServiceImplTest {
File file = this.wxService.getQrcodeService().qrCodePicture(ticket); File file = this.wxService.getQrcodeService().qrCodePicture(ticket);
Assert.assertNotNull(file); Assert.assertNotNull(file);
System.out.println(file.getAbsolutePath()); System.out.println(file.getAbsolutePath());
try {
FileUtils.copyFile(file,new File("d:\\t.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
} }
public void testQrCodePictureUrl() throws WxErrorException { public void testQrCodePictureUrl() throws WxErrorException {

View File

@ -6,7 +6,6 @@ import com.thoughtworks.xstream.XStream;
import me.chanjar.weixin.common.util.xml.XStreamInitializer; import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl; import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
import java.io.IOException; import java.io.IOException;