🎨 增加网络文件转File方法
This commit is contained in:
parent
6919c5a02e
commit
d907b78960
@ -3,7 +3,10 @@ package utils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Base64;
|
||||
@ -62,4 +65,49 @@ public class FileUtils {
|
||||
assert multipartFile != null;
|
||||
multipartFile.transferTo(dest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 网络文件转File
|
||||
* @param url
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static File getFile(String url) throws Exception {
|
||||
//对本地文件命名
|
||||
String fileName = url.substring(url.lastIndexOf("."),url.length());
|
||||
File file = null;
|
||||
|
||||
URL urlfile;
|
||||
InputStream inStream = null;
|
||||
OutputStream os = null;
|
||||
try {
|
||||
file = File.createTempFile("net_url", fileName);
|
||||
//下载
|
||||
urlfile = new URL(url);
|
||||
inStream = urlfile.openStream();
|
||||
os = new FileOutputStream(file);
|
||||
|
||||
int bytesRead = 0;
|
||||
byte[] buffer = new byte[8192];
|
||||
while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (null != os) {
|
||||
os.close();
|
||||
}
|
||||
if (null != inStream) {
|
||||
inStream.close();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user