123333
This commit is contained in:
200
ruoyi-cai/src/main/java/com/ruoyi/cai/util/FileUtils.java
Normal file
200
ruoyi-cai/src/main/java/com/ruoyi/cai/util/FileUtils.java
Normal file
@@ -0,0 +1,200 @@
|
||||
package com.ruoyi.cai.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
* <p>created on 2022/6/16 11:54</p>
|
||||
* @author ZL
|
||||
*/
|
||||
@Slf4j
|
||||
public class FileUtils {
|
||||
|
||||
private final static String TEMP_PATH = "/temp";
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String targetFile = "C:\\ffmpeg\\video\\55585858.mp4";
|
||||
// String filePath = "http://oss.nohi.vip/nono/20220816164358-F4A25960-F837-49B1-83F2-D41865FD46F8.mp4";
|
||||
String filePath = "https://download-video.hik-express.com/fc/20220817184523-909E6F0C-1FE3-4D4D-8A4E-296107EF08CD.mp4?Expires=1661168603&OSSAccessKeyId=LTAI5t9GNWcPxHVUMNU3WRqD&Signature=ybHhkkdhAqYt%2F7x07KYNnPDA9OY%3D";
|
||||
byte[] fileStream = getFileStream(filePath);
|
||||
byteToFile(fileStream,new File(targetFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到文件流
|
||||
* @param url 网络图片URL地址
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getFileStream(String url){
|
||||
InputStream inStream = null;
|
||||
try {
|
||||
URL httpUrl = new URL(url);
|
||||
HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(10 * 1000);
|
||||
conn.setReadTimeout(10 * 1000);
|
||||
inStream = conn.getInputStream();//通过输入流获取图片数据
|
||||
return readInputStream(inStream);//得到图片的二进制数据
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if(inStream != null){
|
||||
try {
|
||||
inStream.close();
|
||||
} catch (IOException e) {
|
||||
log.error("流关闭失败",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void downloadPath(String path,File targetFile){
|
||||
InputStream inStream = null;
|
||||
try {
|
||||
URL httpUrl = new URL(path);
|
||||
HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(10 * 1000);
|
||||
conn.setReadTimeout(10 * 1000);
|
||||
inStream = conn.getInputStream();//通过输入流获取图片数据
|
||||
byte[] buffer = new byte[1024];
|
||||
try (OutputStream os = Files.newOutputStream(targetFile.toPath())){
|
||||
int len ;
|
||||
while((len = inStream.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, len);
|
||||
os.flush();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if(inStream != null){
|
||||
try {
|
||||
inStream.close();
|
||||
} catch (IOException e) {
|
||||
log.error("流关闭失败",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从输入流中获取数据
|
||||
* @param inStream 输入流
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] readInputStream(InputStream inStream) throws Exception{
|
||||
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()){
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while( (len=inStream.read(buffer)) != -1 ){
|
||||
outStream.write(buffer, 0, len);
|
||||
}
|
||||
return outStream.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从输入流中获取数据
|
||||
* @param inputStream 输入流
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String readInputStreamByLine(InputStream inputStream) throws Exception{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))){
|
||||
String str;
|
||||
while ((str = br.readLine()) != null) {
|
||||
sb.append(str);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] fileToByte(File file){
|
||||
try (InputStream in = Files.newInputStream(file.toPath())){
|
||||
byte[] data = new byte[in.available()];
|
||||
in.read(data);
|
||||
return data;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("文件转换字节失败",e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void byteToFile(byte[] bytes,File file){
|
||||
File parentFile = file.getParentFile();
|
||||
if(!parentFile.exists()){
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
try (OutputStream outputStream = Files.newOutputStream(file.toPath())){
|
||||
outputStream.write(bytes);
|
||||
}catch (IOException e){
|
||||
throw new RuntimeException("字节写入文件失败!",e);
|
||||
}
|
||||
}
|
||||
|
||||
public static File getTempFile() throws FileNotFoundException {
|
||||
File path = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||
if (!path.exists()) {
|
||||
path = new File("");
|
||||
}
|
||||
File tempLocation = new File(path.getAbsolutePath() + TEMP_PATH);
|
||||
if (!tempLocation.exists()) {
|
||||
tempLocation.mkdirs();
|
||||
}
|
||||
return tempLocation;
|
||||
}
|
||||
|
||||
public static String getRootFIle() throws FileNotFoundException {
|
||||
File path = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||
if (!path.exists()) {
|
||||
path = new File("");
|
||||
}
|
||||
return path.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static boolean deleteFile(File deleteFile){
|
||||
if(deleteFile == null || !deleteFile.isFile()){
|
||||
return false;
|
||||
}
|
||||
return deleteFile.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param rootFile
|
||||
* @param deleteFilePath
|
||||
*/
|
||||
public static void deleteFile(File rootFile, List<String> deleteFilePath){
|
||||
if(!rootFile.exists()){
|
||||
return;
|
||||
}
|
||||
File[] files = rootFile.listFiles();
|
||||
if(files == null){
|
||||
return;
|
||||
}
|
||||
for (File file : files) {
|
||||
if(!deleteFilePath.contains(file.getName())){
|
||||
try {
|
||||
String path = file.getAbsolutePath();
|
||||
if(file.isDirectory()){
|
||||
org.apache.commons.io.FileUtils.deleteDirectory(file);
|
||||
log.info("del dir success. path=[{}]", path);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("清理文件的时候删除失败,filePath={}",file.getParentFile(),e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user