4 changed files with 129 additions and 2 deletions
@ -0,0 +1,56 @@ |
|||||
|
package com.elink.esua.epdc.config; |
||||
|
|
||||
|
import com.elink.esua.epdc.commons.tools.exception.RenException; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.io.*; |
||||
|
|
||||
|
/** |
||||
|
* 接收文件转化File |
||||
|
* Created by liuhongwei on 2019/6/21. |
||||
|
*/ |
||||
|
public class StreamUtils { |
||||
|
|
||||
|
|
||||
|
|
||||
|
public static File conversionFile(MultipartFile file){ |
||||
|
|
||||
|
File toFile =null; |
||||
|
InputStream ins = null; |
||||
|
try { |
||||
|
// 转化字节流
|
||||
|
ins = file.getInputStream(); |
||||
|
// 获取文件名字
|
||||
|
toFile = new File(file.getOriginalFilename()); |
||||
|
// 字节转化文件
|
||||
|
inputStreamToFile(ins, toFile); |
||||
|
ins.close(); |
||||
|
} catch (IOException e) { |
||||
|
new RenException(500,"文件转化失败"); |
||||
|
} |
||||
|
return toFile; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 流转化 |
||||
|
* @param ins file |
||||
|
* @return |
||||
|
* @author liuhongwei |
||||
|
* @date 2019/6/14 14:07 |
||||
|
*/ |
||||
|
public static void inputStreamToFile(InputStream ins, File file) { |
||||
|
try { |
||||
|
OutputStream os = new FileOutputStream(file); |
||||
|
int bytesRead = 0; |
||||
|
byte[] buffer = new byte[8192]; |
||||
|
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { |
||||
|
os.write(buffer, 0, bytesRead); |
||||
|
} |
||||
|
os.close(); |
||||
|
ins.close(); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue