package tech.liveeasy.biz.camphor.common.utils;
import org.apache.commons.io.FileUtils;
import tech.liveeasy.biz.camphor.common.oss.OSSUnit;
import java.io.File;
import java.net.URL;
public class RecordOSSUtil {
//调用这个方法即可
public static String up2OSS(String url) {
File file = downloadFromUrl(url, "/");
String oss = OSSUnit.uploadObject2OSS(file, "record/");
file.delete();
return oss;
}
public static File downloadFromUrl(String url, String dir) {
try {
URL httpUrl = new URL(url);
String fileName = getFileNameFromUrl(url);
File file = new File(dir + fileName);
FileUtils.copyURLToFile(httpUrl, file);
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getFileNameFromUrl(String url) {
String name = new Long(System.currentTimeMillis()).toString() + ".X";
int index = url.lastIndexOf("/");
if (index > 0) {
name = url.substring(index + 1);
if (name.trim().length() > 0) {
return name;
}
}
return name;
}
}
通过url下载文件保存本地或者转存云服务器
最新推荐文章于 2024-08-26 18:04:05 发布