7 changed files with 92 additions and 114 deletions
@ -1,37 +1,65 @@ |
|||||
package com.epmet.commons.tools.utils; |
package com.epmet.commons.tools.utils; |
||||
|
|
||||
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; |
import java.nio.file.Files; |
||||
|
|
||||
|
/** |
||||
|
* @author kamui |
||||
|
*/ |
||||
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 resultFile = File.createTempFile(name, '.' + ext, tmpDirFile); |
File tmpFile; |
||||
|
if (tmpDirFile == null) { |
||||
|
tmpFile = File.createTempFile(name, '.' + ext); |
||||
|
} else { |
||||
|
tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile); |
||||
|
} |
||||
|
|
||||
|
tmpFile.deleteOnExit(); |
||||
|
FileOutputStream fos = new FileOutputStream(tmpFile); |
||||
|
try { |
||||
|
int read = 0; |
||||
|
byte[] bytes = new byte[1024 * 100]; |
||||
|
while ((read = inputStream.read(bytes)) != -1) { |
||||
|
fos.write(bytes, 0, read); |
||||
|
} |
||||
|
|
||||
|
fos.flush(); |
||||
|
} catch (Exception e) { |
||||
|
|
||||
resultFile.deleteOnExit(); |
} finally { |
||||
org.apache.commons.io.FileUtils.copyToFile(inputStream, resultFile); |
if (fos != null) { |
||||
return resultFile; |
try { |
||||
|
fos.close(); |
||||
|
} catch (IOException e) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
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, Files.createTempDirectory("weixin-java-tools-temp").toFile()); |
return createTmpFile(inputStream, name, ext, null); |
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
Loading…
Reference in new issue