Debug
This commit is contained in:
459
src/main/java/com/eg/www/common/utils/AliYunPanUtil.java
Normal file
459
src/main/java/com/eg/www/common/utils/AliYunPanUtil.java
Normal file
@@ -0,0 +1,459 @@
|
||||
package com.eg.www.common.utils;
|
||||
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.core.io.file.FileWriter;
|
||||
import cn.hutool.core.io.watch.WatchMonitor;
|
||||
import cn.hutool.core.io.watch.Watcher;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.setting.Setting;
|
||||
import com.eg.www.common.CommonConstants;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Description: 操作阿里云盘工具类
|
||||
* Author: Eg
|
||||
* Date: 2021/08/29
|
||||
*/
|
||||
public class AliYunPanUtil{
|
||||
|
||||
//配置文件
|
||||
Setting setting =CommonConstants.setting;
|
||||
//Token文本框
|
||||
JTextField tokenText = CommonConstants.tokenText;
|
||||
// 开始备份
|
||||
private JButton startBackup = CommonConstants.startBackup;
|
||||
//请求工具类
|
||||
OkHttpUtil okHttpUtil = new OkHttpUtil();
|
||||
FileWriter writerLog = CommonConstants.writerLog;
|
||||
FileReader readerLog = CommonConstants.readerLog;
|
||||
|
||||
/**
|
||||
* 开始备份
|
||||
*/
|
||||
public void startBackup() {
|
||||
CommonConstants.CLEAN_CONSOLE=1;
|
||||
CommonConstants.BACK_STATE = true;
|
||||
boolean login = this.getAliYunPanInfo();//登录阿里云
|
||||
if (!login) return;
|
||||
if (!checkConfig()) return;
|
||||
CommonConstants.FILE_ID = this.getFileId(CommonConstants.ROOT, CommonConstants.BACK_NAME);//备份目录ID
|
||||
if (CommonConstants.BACK_TYPE==0){//普通备份
|
||||
this.scanFolders(CommonConstants.PATH,CommonConstants.FILE_ID,true);
|
||||
}else {
|
||||
//开始获取文件
|
||||
List<String> folderList = FileUtil.fileFolderList(CommonConstants.PATH,FileUtil.FOLDER);//获取用户目录下所有目录
|
||||
List<String> folderFileList = FileUtil.fileFolderList(CommonConstants.PATH,FileUtil.FILE);//本地文件夹下文件
|
||||
|
||||
//上传文件夹下文件
|
||||
if (folderFileList.size()!=0){
|
||||
CommonConstants.addConsole("获取:"+CommonConstants.PATH+" 下所有文件成功");
|
||||
uploadFileList(folderFileList,CommonConstants.FILE_ID,true);
|
||||
}
|
||||
|
||||
//上传文件夹下所有目录
|
||||
if (folderList.size()!=0){
|
||||
for (String folderName : folderList) {
|
||||
String path = CommonConstants.PATH + FileUtil.FILE_SEPARATOR + folderName;//路径
|
||||
uploadTwoLevelFolder(CommonConstants.FILE_ID,path);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
CommonConstants.addConsole("本次备份:"+CommonConstants.PATH+" 下所有文件成功!...");
|
||||
startBackup.setText("开始备份");
|
||||
startBackup.setEnabled(true);
|
||||
CommonConstants.BACK_STATE = false;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传二级文件夹
|
||||
* @param fileId
|
||||
* @param path
|
||||
*/
|
||||
private void uploadTwoLevelFolder(String fileId, String path) {
|
||||
CommonConstants.addConsole("开始获取:"+path);
|
||||
List<String> fileList = FileUtil.fileFolderList(path,FileUtil.FILE);//本地文件夹下文件
|
||||
uploadFileList(fileList,fileId,true);
|
||||
String folderFileId = this.getFileId(fileId, "文件夹");//微信备份-文件夹
|
||||
String dateFileId = this.getFileId(folderFileId, getFolderName(path));//微信备份-文件夹-folderName
|
||||
this.scanFolders(path,dateFileId,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取阿里云用户信息
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public boolean getAliYunPanInfo(){
|
||||
CommonConstants.addConsole("开始登录阿里云盘...");
|
||||
CommonConstants.TOKEN="";
|
||||
JSONObject data = new JSONObject();
|
||||
data.set("refresh_token",CommonConstants.REFRESH_TOKEN);
|
||||
JSONObject aliYunPanInfo = okHttpUtil.doPost(CommonConstants.TOKEN_URL, data);
|
||||
if (ObjectUtil.isNull(aliYunPanInfo)){
|
||||
CommonConstants.addConsole("登录失败...请检查Token填写是否正确...");
|
||||
return false;
|
||||
}
|
||||
CommonConstants.TOKEN = aliYunPanInfo.getStr("token_type") + " " + aliYunPanInfo.getStr("access_token");
|
||||
CommonConstants.DriveId = aliYunPanInfo.getStr("default_drive_id");
|
||||
CommonConstants.REFRESH_TOKEN = aliYunPanInfo.getStr("refresh_token");
|
||||
setting.set("tokenText",aliYunPanInfo.getStr("refresh_token"));
|
||||
tokenText.setText(aliYunPanInfo.getStr("refresh_token"));
|
||||
setting.store(CommonConstants.CONFIG_PATH);
|
||||
setting.autoLoad(true);
|
||||
if (StrUtil.isNotEmpty(CommonConstants.TOKEN)){
|
||||
CommonConstants.addConsole("登录阿里云盘成功...");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取阿里云文件列表
|
||||
* @param fileId
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject getFileList(String fileId){
|
||||
JSONObject data = new JSONObject();
|
||||
data.set("drive_id",CommonConstants.DriveId);
|
||||
data.set("parent_file_id",fileId);
|
||||
data.set("limit",100);
|
||||
data.set("all",false);
|
||||
data.set("image_thumbnail_process","image/resize,w_400/format,jpeg");
|
||||
data.set("image_url_process","image/resize,w_1920/format,jpeg");
|
||||
data.set("video_thumbnail_process","video/snapshot,t_0,f_jpg,ar_auto,w_300");
|
||||
data.set("fields","*");
|
||||
data.set("order_by","updated_at");
|
||||
data.set("order_direction","DESC");
|
||||
return okHttpUtil.doPost(CommonConstants.FILE_LIST_URL,data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件夹
|
||||
* @param fileId 父级目录ID 根目录为root
|
||||
* @param name 新文件夹名称
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject createFolder(String fileId,String name){
|
||||
//创建文件夹
|
||||
JSONObject data = new JSONObject();
|
||||
data.set("drive_id",CommonConstants.DriveId);
|
||||
data.set("parent_file_id",fileId);
|
||||
data.set("name",name);
|
||||
data.set("type","folder");
|
||||
data.set("check_name_mode","refuse");
|
||||
return okHttpUtil.doPost(CommonConstants.CREATE_FILE_URL,data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到阿里云盘
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject uploadFile(String fileId, Map<String, Object> fileInfo){
|
||||
JSONObject data = new JSONObject();
|
||||
JSONObject list = new JSONObject();
|
||||
JSONArray array = new JSONArray();
|
||||
list.set("part_number",1);
|
||||
array.add(list);
|
||||
data.set("drive_id",CommonConstants.DriveId);
|
||||
data.set("name",fileInfo.get("name"));
|
||||
data.set("type","file");
|
||||
data.set("content_type",fileInfo.get("content_type"));
|
||||
data.set("size",fileInfo.get("size"));
|
||||
data.set("parent_file_id",fileId);
|
||||
data.set("part_info_list",array);
|
||||
data.set("content_hash_name","sha1");
|
||||
data.set("content_hash",fileInfo.get("content_hash"));
|
||||
data.set("ignoreError",false);
|
||||
data.set("check_name_mode","refuse");
|
||||
return okHttpUtil.doFilePost(CommonConstants.CREATE_FILE_URL,data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成文件上传
|
||||
* @param fileId
|
||||
* @param uploadId
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject completeFile(String fileId,String uploadId){
|
||||
JSONObject data = new JSONObject();
|
||||
JSONArray array = new JSONArray();
|
||||
data.set("drive_id",CommonConstants.DriveId);
|
||||
data.set("file_id",fileId);
|
||||
data.set("ignoreError",true);
|
||||
data.set("part_info_list",array);
|
||||
data.set("upload_id",uploadId);
|
||||
return okHttpUtil.doPost(CommonConstants.COMPLETE_FILE_URL,data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除阿里云盘文件
|
||||
* @param fileId
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject deleteFile(String fileId){
|
||||
JSONObject data = new JSONObject();
|
||||
data.set("drive_id",CommonConstants.DriveId);
|
||||
data.set("file_id",fileId);
|
||||
return okHttpUtil.doPost(CommonConstants.DELETE_FILE_URL,data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件夹ID
|
||||
* @param parentFileId 父级文件夹ID
|
||||
* @param folderName 文件夹名称
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getFileId(String parentFileId,String folderName){
|
||||
//CommonConstants.addConsole("开始获取文件夹:"+folderName);
|
||||
String fileId="";
|
||||
JSONObject fileList = getFileList(parentFileId);//获取文件目录
|
||||
JSONArray fileArray = fileList.getJSONArray("items");
|
||||
if (ObjectUtil.isNotNull(fileArray) && fileArray.size()>0){
|
||||
for (int i = 0; i < fileArray.size(); i++) {
|
||||
JSONObject folder = fileArray.getJSONObject(i);
|
||||
if ("folder".equals(folder.getStr("type")) && folderName.equals(folder.getStr("name"))){
|
||||
fileId = folder.getStr("file_id");
|
||||
return fileId;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (StrUtil.isEmpty(fileId)){
|
||||
JSONObject folder = createFolder(parentFileId, folderName); //创建备份目录
|
||||
fileId=folder.getStr("file_id");
|
||||
}
|
||||
return fileId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行文件上传
|
||||
* @param fileId 文件夹ID
|
||||
* @param fileInfo 文件信息
|
||||
* @throws Exception
|
||||
*/
|
||||
public void doUploadFile(String fileId,Map<String, Object> fileInfo){
|
||||
//写入上传文件的路径
|
||||
List<String> logList = readerLog.readLines();
|
||||
if (!logList.contains(fileInfo.get("path"))){
|
||||
CommonConstants.addConsole("开始上传:"+fileInfo.get("path"));
|
||||
JSONObject uploadFile = uploadFile(fileId,fileInfo);
|
||||
if(ObjectUtil.isNotNull(uploadFile.getJSONArray("part_info_list"))){//上传新文件
|
||||
byte[] fileBytes = FileUtil.readBytes(fileInfo.get("path").toString());
|
||||
String uploadUrl = uploadFile.getJSONArray("part_info_list").getJSONObject(0).getStr("upload_url");
|
||||
okHttpUtil.uploadFileBytes(uploadUrl,fileBytes);
|
||||
}
|
||||
if (StrUtil.isEmpty(uploadFile.getStr("exist"))){//上传完成
|
||||
String upFileId = uploadFile.getStr("file_id");
|
||||
String uploadId = uploadFile.getStr("upload_id");
|
||||
completeFile(upFileId, uploadId);
|
||||
}
|
||||
writerLog.append(fileInfo.get("path")+ "\n");
|
||||
CommonConstants.addConsole("上传文件成功:"+fileInfo.get("name"));
|
||||
}else {
|
||||
CommonConstants.addConsole(fileInfo.get("path")+" 已上传 跳过");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描子目录
|
||||
* @param path 本地路径
|
||||
* @param pathId 备份目录 ID
|
||||
* @param isUploadFile 是否上传文件
|
||||
* @throws Exception
|
||||
*/
|
||||
public void scanFolders(String path,String pathId,Boolean isUploadFile){
|
||||
CommonConstants.addConsole("开始获取:"+path);
|
||||
//获取文件夹下所有文件
|
||||
List<String> fileList = FileUtil.fileFolderList(path,FileUtil.FILE);
|
||||
//CommonConstants.addConsole("获取:"+path+" 下所有文件成功");
|
||||
if (isUploadFile){
|
||||
uploadFileList(fileList,pathId,false);
|
||||
}
|
||||
//获得目录下所有文件夹
|
||||
List<String> folderList = FileUtil.fileFolderList(path,FileUtil.FOLDER);
|
||||
//循环文件夹
|
||||
for (String folder : folderList){
|
||||
String fileId = getFileId(pathId, folder);//创建文件夹-文件夹ID
|
||||
String filePath = path + FileUtil.FILE_SEPARATOR + folder;//路径
|
||||
fileList = FileUtil.fileFolderList(path,FileUtil.FILE);//获取当前文件夹下所有文件
|
||||
uploadFileList(fileList,fileId,false);//上传当前文件夹内的文件
|
||||
//CommonConstants.addConsole("扫描新文件夹:"+filePath);
|
||||
scanFolders(filePath,fileId,true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件夹下文件到某个目录
|
||||
* @param fileList 文件list
|
||||
* @param pathId 阿里云文件夹ID
|
||||
* @param backType 是否开启分类
|
||||
* @throws Exception
|
||||
*/
|
||||
public void uploadFileList(List<String> fileList, String pathId,Boolean backType){
|
||||
List<String> logList = readerLog.readLines();
|
||||
fileList.removeAll(logList);
|
||||
for (String filePath : fileList) {
|
||||
try {
|
||||
Map<String, Object> map = FileUtil.getFileInfo(filePath);
|
||||
if (backType){//开启分类
|
||||
String type = map.get("type").toString();
|
||||
String typeFileId=this.getFileId(pathId,type);//微信备份-类型
|
||||
String dateFileId=this.getFileId(typeFileId,getFolderName(filePath));//微信备份-类型-文件夹
|
||||
doUploadFile(dateFileId,map);
|
||||
}else {
|
||||
doUploadFile(pathId,map);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CommonConstants.addConsole("遇到异常情况:"+e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件夹名称
|
||||
* @param thisPath
|
||||
* @return
|
||||
*/
|
||||
public String getFolderName(String thisPath) {
|
||||
String folderName = StrUtil.subAfter(thisPath , CommonConstants.PATH+"\\", false);
|
||||
if (StrUtil.isNotEmpty(folderName) && !folderName.contains("\\")){
|
||||
//写入文件目录
|
||||
//writerLog.append(CommonConstants.PATH + FileUtil.FILE_SEPARATOR+folderName + "");
|
||||
}else{
|
||||
folderName = StrUtil.subBefore(folderName , "\\", false);
|
||||
}
|
||||
return folderName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 监控目录
|
||||
*/
|
||||
public void monitorFolder() {
|
||||
if (StrUtil.isEmpty(setting.getStr("pathText"))) return;
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
CommonConstants.CLEAN_CONSOLE=1;
|
||||
//开启目录检测 开始获取文件夹
|
||||
Console.log("开启目录检测");
|
||||
CommonConstants.addConsole("开启目录检测");
|
||||
File file = FileUtil.file(setting.getStr("pathText"));
|
||||
//这里只监听文件或目录的修改事件
|
||||
WatchMonitor watchMonitor = WatchMonitor.create(file,WatchMonitor.ENTRY_CREATE,WatchMonitor.ENTRY_MODIFY);
|
||||
watchMonitor.setWatcher(new Watcher(){
|
||||
@Override
|
||||
public void onCreate(WatchEvent<?> event, Path currentPath) {
|
||||
Object obj = event.context();
|
||||
Console.log("创建:{}-> {}", currentPath, obj);
|
||||
//备份方法不执行时候执行监听
|
||||
if (!CommonConstants.BACK_STATE){
|
||||
uploadMonitor(currentPath,obj);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onModify(WatchEvent<?> event, Path currentPath) {
|
||||
Object obj = event.context();
|
||||
Console.log("修改:{}-> {}", currentPath, obj);
|
||||
//备份方法不执行时候执行监听
|
||||
if (!CommonConstants.BACK_STATE){
|
||||
String path = currentPath.toString() + "\\" + obj.toString();
|
||||
//CommonConstants.addConsole("检测到:"+path+" 文件发生变化!");
|
||||
List<String> logList = readerLog.readLines();
|
||||
logList.remove(path);
|
||||
writerLog.writeLines(logList);
|
||||
}
|
||||
//uploadMonitor(currentPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDelete(WatchEvent<?> event, Path currentPath) {
|
||||
/*Object obj = event.context();
|
||||
Console.log("删除:{}-> {}", currentPath, obj);*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOverflow(WatchEvent<?> event, Path currentPath) {
|
||||
/*Object obj = event.context();
|
||||
Console.log("Overflow:{}-> {}", currentPath, obj);*/
|
||||
}
|
||||
});
|
||||
//设置监听目录的最大深入,目录层级大于制定层级的变更将不被监听,默认只监听当前层级目录
|
||||
//监听所有目录
|
||||
watchMonitor.setMaxDepth(Integer.MAX_VALUE);
|
||||
//启动监听
|
||||
watchMonitor.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传监控目录
|
||||
* @param currentPath
|
||||
*/
|
||||
public void uploadMonitor(Path currentPath,Object obj) {
|
||||
CommonConstants.addConsole("检测到:"+currentPath.toString()+" 目录有新文件...");
|
||||
if (checkConfig()){
|
||||
Thread backup = new Thread(() -> {
|
||||
boolean login = getAliYunPanInfo();//登录阿里云
|
||||
if (!login){
|
||||
return;
|
||||
}
|
||||
CommonConstants.FILE_ID = getFileId(CommonConstants.ROOT, CommonConstants.BACK_NAME);//备份目录ID
|
||||
uploadTwoLevelFolder(CommonConstants.FILE_ID,currentPath.toString());
|
||||
CommonConstants.addConsole("上传 "+obj.toString()+" 成功!");
|
||||
});
|
||||
backup.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证配置文件
|
||||
*/
|
||||
public Boolean checkConfig(){
|
||||
if (StrUtil.isEmpty(setting.getStr("pathText"))){
|
||||
CommonConstants.addConsole("您没有选择需要备份的目录");
|
||||
return false;
|
||||
}
|
||||
if (StrUtil.isEmpty(setting.getStr("tokenText"))){
|
||||
CommonConstants.addConsole( "您没有输入阿里云token");
|
||||
return false;
|
||||
}
|
||||
if (StrUtil.isEmpty(setting.getStr("folderText"))){
|
||||
CommonConstants.addConsole("您没有输入需要备份到阿里云的目录");
|
||||
return false;
|
||||
}
|
||||
if (setting.getStr("tokenText").length()!=32){
|
||||
CommonConstants.addConsole("您输入的token不正确");
|
||||
return false;
|
||||
}
|
||||
if (!FileUtil.isDirectory(setting.getStr("pathText"))){
|
||||
CommonConstants.addConsole("请选择正确目录");
|
||||
return false;
|
||||
}
|
||||
CommonConstants.PATH = setting.getStr("pathText");
|
||||
CommonConstants.REFRESH_TOKEN = setting.getStr("tokenText");
|
||||
CommonConstants.BACK_NAME = setting.getStr("folderText");
|
||||
CommonConstants.BACK_TYPE = Integer.parseInt(setting.getStr("backType"));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
178
src/main/java/com/eg/www/common/utils/FileUtil.java
Normal file
178
src/main/java/com/eg/www/common/utils/FileUtil.java
Normal file
@@ -0,0 +1,178 @@
|
||||
package com.eg.www.common.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
|
||||
import javax.activation.MimetypesFileTypeMap;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Description: 文件操作工具类
|
||||
* Author: Eg
|
||||
* Date: 2021/08/29
|
||||
*/
|
||||
public class FileUtil extends cn.hutool.core.io.FileUtil {
|
||||
|
||||
public static final String FILE="File";//文件
|
||||
public static final String FOLDER="Folder";//文件夹
|
||||
static List<String> docTypes = new ArrayList<>();//文档类型
|
||||
static List<String> compressTypes = new ArrayList<>();//压缩包类型
|
||||
static List<String> imageTypes = new ArrayList<>();//图片类型
|
||||
static List<String> musicTypes = new ArrayList<>();//音乐类型
|
||||
static List<String> videoTypes = new ArrayList<>();//视频类型
|
||||
static List<String> applyTypes = new ArrayList<>();//软件类型
|
||||
static List<String> devTypes = new ArrayList<>();//开发类型
|
||||
|
||||
//文件类型
|
||||
static {
|
||||
/**
|
||||
* 文档类型
|
||||
*/
|
||||
docTypes.add("xls");
|
||||
docTypes.add("xlsx");
|
||||
docTypes.add("xlsm");
|
||||
docTypes.add("doc");
|
||||
docTypes.add("docx");
|
||||
docTypes.add("ppt");
|
||||
docTypes.add("pptx");
|
||||
docTypes.add("txt");
|
||||
docTypes.add("pdf");
|
||||
docTypes.add("chm");
|
||||
docTypes.add("xmind");
|
||||
|
||||
/**
|
||||
* 压缩包类型
|
||||
*/
|
||||
compressTypes.add("zip");
|
||||
compressTypes.add("rar");
|
||||
compressTypes.add("7z");
|
||||
compressTypes.add("tar.gz");
|
||||
compressTypes.add("gz");
|
||||
|
||||
/**
|
||||
* 图片类型
|
||||
*/
|
||||
imageTypes.add("png");
|
||||
imageTypes.add("jpg");
|
||||
imageTypes.add("jpeg");
|
||||
imageTypes.add("gif");
|
||||
imageTypes.add("ico");
|
||||
imageTypes.add("psd");
|
||||
|
||||
/**
|
||||
* 音频类型
|
||||
*/
|
||||
musicTypes.add("mp3");
|
||||
musicTypes.add("wma");
|
||||
musicTypes.add("aac");
|
||||
musicTypes.add("m4a");
|
||||
musicTypes.add("amr");
|
||||
|
||||
/**
|
||||
* 视频类型
|
||||
*/
|
||||
videoTypes.add("mp4");
|
||||
videoTypes.add("avi");
|
||||
videoTypes.add("3gp");
|
||||
videoTypes.add("flv");
|
||||
videoTypes.add("mkv");
|
||||
videoTypes.add("mov");
|
||||
videoTypes.add("wmv");
|
||||
|
||||
/**
|
||||
* 软件类型
|
||||
*/
|
||||
applyTypes.add("1");
|
||||
applyTypes.add("apk");
|
||||
applyTypes.add("exe");
|
||||
applyTypes.add("ipa");
|
||||
applyTypes.add("app");
|
||||
applyTypes.add("dmg");
|
||||
applyTypes.add("rpm");
|
||||
|
||||
/**
|
||||
* 开发类型
|
||||
*/
|
||||
devTypes.add("properties");
|
||||
devTypes.add("java");
|
||||
devTypes.add("php");
|
||||
devTypes.add("jsp");
|
||||
devTypes.add("js");
|
||||
devTypes.add("vue");
|
||||
devTypes.add("sh");
|
||||
devTypes.add("sql");
|
||||
devTypes.add("xml");
|
||||
devTypes.add("html");
|
||||
devTypes.add("conf");
|
||||
devTypes.add("jar");
|
||||
devTypes.add("bat");
|
||||
devTypes.add("yml");
|
||||
devTypes.add("css");
|
||||
devTypes.add("json");
|
||||
devTypes.add("out");
|
||||
devTypes.add("class");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型
|
||||
* @param name 文件名
|
||||
* @return 返回文件类型
|
||||
*/
|
||||
public static String getFileTypes(String name){
|
||||
//获取文件的后缀名
|
||||
String suffix = getSuffix(name).toLowerCase();
|
||||
if (docTypes.contains(suffix)) return "文档";
|
||||
if (imageTypes.contains(suffix)) return "图片";
|
||||
if (musicTypes.contains(suffix)) return "音频";
|
||||
if (videoTypes.contains(suffix)) return "视频";
|
||||
if (applyTypes.contains(suffix)) return "软件";
|
||||
if (devTypes.contains(suffix)) return "开发";
|
||||
if (compressTypes.contains(suffix)) return "压缩包";
|
||||
return "其它";
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取目录下的文件 或 文件夹
|
||||
* 不含子目录
|
||||
* @param path 目录绝对路径或者相对路径
|
||||
* @param type 文件或文件夹 为空默认返回文件夹
|
||||
* @return 列表
|
||||
*/
|
||||
public static List<String> fileFolderList(String path,String type) {
|
||||
List<String> List = new ArrayList<>();
|
||||
final File[] files = ls(path);
|
||||
for (File file : files) {
|
||||
if(FILE.equals(type) && file.isFile()){
|
||||
List.add(file.getPath());
|
||||
}
|
||||
if (FOLDER.equals(type) && file.isDirectory()){
|
||||
List.add(file.getName());
|
||||
}
|
||||
}
|
||||
return List;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件信息
|
||||
* @param path 文件路径
|
||||
*/
|
||||
public static Map<String, Object> getFileInfo(String path) {
|
||||
String contentType= new MimetypesFileTypeMap().getContentType(new File(path));
|
||||
if (StrUtil.isEmpty(contentType)) contentType="application/octet-stream";
|
||||
Map<String, Object> map = new HashMap();
|
||||
File file = file(path);
|
||||
String type = getFileTypes(file.getName());
|
||||
map.put("name",file.getName());
|
||||
map.put("path",file.getPath());
|
||||
map.put("size",file.length());
|
||||
map.put("type",type);
|
||||
map.put("content_type",contentType);
|
||||
map.put("content_hash",DigestUtil.sha1Hex(file).toUpperCase());
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
127
src/main/java/com/eg/www/common/utils/OkHttpUtil.java
Normal file
127
src/main/java/com/eg/www/common/utils/OkHttpUtil.java
Normal file
@@ -0,0 +1,127 @@
|
||||
package com.eg.www.common.utils;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import okhttp3.*;
|
||||
import com.eg.www.common.CommonConstants;
|
||||
import com.eg.www.common.MyConsole;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Description: http请求工具类
|
||||
* Author: Eg
|
||||
* Date: 2021/08/29
|
||||
*/
|
||||
public class OkHttpUtil {
|
||||
|
||||
// 日志界面
|
||||
MyConsole console = CommonConstants.console;
|
||||
|
||||
static OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(5, TimeUnit.MINUTES).build();
|
||||
static MediaType mediaType = MediaType.parse("application/json");
|
||||
static RequestBody body;
|
||||
static Request request;
|
||||
|
||||
|
||||
/**
|
||||
* 阿里云盘交互的POST请求
|
||||
* @param url
|
||||
* @param data
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject doPost(String url, JSONObject data){
|
||||
int errNum=0;
|
||||
try {
|
||||
body = RequestBody.create(mediaType,data.toString());
|
||||
request = new Request.Builder()
|
||||
.url(url)
|
||||
.method("POST", body)
|
||||
.addHeader("authorization", CommonConstants.TOKEN)
|
||||
.addHeader("Content-Type", "application/json").build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String result = response.body().string();
|
||||
CommonConstants.addConsole("请求状态码:"+response.code());
|
||||
//System.out.println("result:>>>>>>>>>>>>>>>>>>>"+result);
|
||||
return JSONUtil.parseObj(result);
|
||||
} catch (Exception e) {
|
||||
if (e.toString().contains("A JSONObject text")){
|
||||
CommonConstants.addConsole("普通请求遇到异常:"+e.toString());
|
||||
return null;
|
||||
}else {
|
||||
errNum++;
|
||||
CommonConstants.addConsole("普通请求遇到异常:"+e.toString());
|
||||
if (errNum>5){
|
||||
CommonConstants.addConsole("普通请求失败次数超过:"+errNum+" 次....已停止");
|
||||
return null;
|
||||
}else{
|
||||
CommonConstants.addConsole("普通请求发起第:"+errNum+" 次重试");
|
||||
return doPost(url,data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 阿里云盘上传文件的POST请求
|
||||
* @param url
|
||||
* @param data
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public JSONObject doFilePost(String url,JSONObject data){
|
||||
int errNum=0;
|
||||
try {
|
||||
RequestBody body = RequestBody.create(mediaType, data.toString());
|
||||
request = new Request.Builder()
|
||||
.url(url)
|
||||
.method("POST", body)
|
||||
.addHeader("authorization",CommonConstants.TOKEN)
|
||||
.addHeader("Content-Type", "multipart/form-data").build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String result = response.body().string();
|
||||
CommonConstants.addConsole("请求状态码:"+response.code());
|
||||
//System.out.println("result:>>>>>>>>>>>>>>>>>>>"+result);
|
||||
return JSONUtil.parseObj(result);
|
||||
} catch (Exception e) {
|
||||
errNum++;
|
||||
CommonConstants.addConsole("上传请求遇到异常:"+e.toString());
|
||||
if (errNum>5){
|
||||
CommonConstants.addConsole("上传请求失败次数超过:"+errNum+" 次....已停止");
|
||||
return null;
|
||||
}else{
|
||||
CommonConstants.addConsole("上传请求发起第:"+errNum+" 次重试");
|
||||
return doFilePost(url,data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 阿里云盘上传二进制文件
|
||||
* @param url
|
||||
* @param fileBytes
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public void uploadFileBytes(String url,byte[] fileBytes){
|
||||
int errNum=0;
|
||||
try {
|
||||
RequestBody body = RequestBody.create(fileBytes);
|
||||
Request request = new Request.Builder().url(url).method("PUT",body).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
//String result=response.body().string();
|
||||
CommonConstants.addConsole("上传文件请求状态码:"+response.code());
|
||||
} catch (Exception e) {
|
||||
errNum++;
|
||||
CommonConstants.addConsole("上传文件遇到异常:"+e.toString());
|
||||
if (errNum>5){
|
||||
CommonConstants.addConsole("上传文件失败次数超过:"+errNum+" 次....已停止");
|
||||
return;
|
||||
}else{
|
||||
CommonConstants.addConsole("上传文件发起第:"+errNum+" 次重试");
|
||||
uploadFileBytes(url,fileBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
src/main/java/com/eg/www/common/utils/getToken.java
Normal file
10
src/main/java/com/eg/www/common/utils/getToken.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.eg.www.common.utils;
|
||||
|
||||
/**
|
||||
* @author: Eg
|
||||
* @Date: 2021/8/29 17:50
|
||||
*/
|
||||
public class getToken {
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user