mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-29 09:38:19 +08:00
增加临时文件目录配置
在下载多媒体文件下载时可以指定临时文件目录。
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
@ -59,7 +71,7 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user