import org.apache.commons.io.FileUtils;
public String downloadFromUrl(String url) { String filePath = CommonUtils.getProjectPath() + /file; if (CommonUtils.isOSLinux()) { filePath = filePath.split(":")[1]; } String fileName = UUID.randomUUID().toString() + ".png"; try { URL videoUrl = new URL(url); File file = new File(filePath, fileName); FileUtils.copyURLToFile(videoUrl,file); } catch (Exception e) { log.info("文件下载失败URL:{}",url); } return filePath + "/" + fileName; }
/** * 获取项目部署跟路径 * @return */ public static String getProjectPath(){ try { return new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent(); } catch (IOException e) { e.printStackTrace(); } return null; }
/** * 判断服务器是windows还是linux @return */ public static boolean isOSLinux(){ Properties prop = System.getProperties(); String property = prop.getProperty("os.name"); if(null != property && property.toLowerCase().indexOf("linux")>-1){ return true; }else { return false; } }