Browse Source

#591 文件上传接口不自动关闭inputStream,由调用方自己控制

master
Binary Wang 6 years ago
parent
commit
f574403445
4 changed files with 31 additions and 27 deletions
  1. +1
    -1
      weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java
  2. +11
    -11
      weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMediaDownloadRequestExecutor.java
  3. +15
    -11
      weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpMediaDownloadRequestExecutor.java
  4. +4
    -4
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java

+ 1
- 1
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java View File

@@ -19,7 +19,7 @@ public class FileUtils {
File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile); File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile);


resultFile.deleteOnExit(); resultFile.deleteOnExit();
org.apache.commons.io.FileUtils.copyInputStreamToFile(inputStream, resultFile);
org.apache.commons.io.FileUtils.copyToFile(inputStream, resultFile);
return resultFile; return resultFile;
} }




+ 11
- 11
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMediaDownloadRequestExecutor.java View File

@@ -1,11 +1,9 @@
package me.chanjar.weixin.common.util.http.apache; package me.chanjar.weixin.common.util.http.apache;


import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header; import org.apache.http.Header;
@@ -16,9 +14,12 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.RequestHttp;


/** /**
* Created by ecoolper on 2017/5/5. * Created by ecoolper on 2017/5/5.
@@ -45,8 +46,7 @@ public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
} }


try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet); try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet);
InputStream inputStream = InputStreamResponseHandler.INSTANCE
.handleResponse(response)) {
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
Header[] contentTypeHeader = response.getHeaders("Content-Type"); Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) { if (contentTypeHeader != null && contentTypeHeader.length > 0) {
if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) { if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) {


+ 15
- 11
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpMediaDownloadRequestExecutor.java View File

@@ -1,5 +1,13 @@
package me.chanjar.weixin.common.util.http.jodd; package me.chanjar.weixin.common.util.http.jodd;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import jodd.http.HttpConnectionProvider; import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest; import jodd.http.HttpRequest;
import jodd.http.HttpResponse; import jodd.http.HttpResponse;
@@ -8,16 +16,9 @@ import jodd.util.StringPool;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils; import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor; import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;


/** /**
* Created by ecoolper on 2017/5/5. * Created by ecoolper on 2017/5/5.
@@ -57,9 +58,12 @@ public class JoddHttpMediaDownloadRequestExecutor extends BaseMediaDownloadReque
return null; return null;
} }


InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
return FileUtils.createTmpFile(inputStream, FilenameUtils.getBaseName(fileName), FilenameUtils.getExtension(fileName),
super.tmpDirFile);
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
return FileUtils.createTmpFile(inputStream,
FilenameUtils.getBaseName(fileName),
FilenameUtils.getExtension(fileName),
super.tmpDirFile);
}
} }






+ 4
- 4
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java View File

@@ -1,12 +1,12 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;


import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;

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 me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;

/** /**
* <pre> * <pre>
* 媒体管理接口 * 媒体管理接口
@@ -30,7 +30,7 @@ public interface WxCpMediaService {
* *
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts} * @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param inputStream 输入流
* @param inputStream 输入流,需要调用方控制关闭该输入流
*/ */
WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputStream) WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputStream)
throws WxErrorException, IOException; throws WxErrorException, IOException;


Loading…
Cancel
Save