-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a58717
commit 2914942
Showing
4 changed files
with
37 additions
and
37 deletions.
There are no files selected for viewing
31 changes: 8 additions & 23 deletions
31
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,37 @@ | ||
package me.chanjar.weixin.common.util.fs; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
|
||
public class FileUtils { | ||
|
||
|
||
/** | ||
* 创建临时文件 | ||
* | ||
* @param inputStream | ||
* @param inputStream 输入文件流 | ||
* @param name 文件名 | ||
* @param ext 扩展名 | ||
* @param tmpDirFile 临时文件夹目录 | ||
*/ | ||
public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException { | ||
File tmpFile; | ||
if (tmpDirFile == null) { | ||
tmpFile = File.createTempFile(name, '.' + ext); | ||
} else { | ||
tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile); | ||
} | ||
|
||
tmpFile.deleteOnExit(); | ||
|
||
try (FileOutputStream fos = new FileOutputStream(tmpFile)) { | ||
int read = 0; | ||
byte[] bytes = new byte[1024 * 100]; | ||
while ((read = inputStream.read(bytes)) != -1) { | ||
fos.write(bytes, 0, read); | ||
} | ||
File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile); | ||
|
||
fos.flush(); | ||
return tmpFile; | ||
} | ||
resultFile.deleteOnExit(); | ||
org.apache.commons.io.FileUtils.copyInputStreamToFile(inputStream, resultFile); | ||
return resultFile; | ||
} | ||
|
||
/** | ||
* 创建临时文件 | ||
* | ||
* @param inputStream | ||
* @param inputStream 输入文件流 | ||
* @param name 文件名 | ||
* @param ext 扩展名 | ||
*/ | ||
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters