|
|
|
@ -13,6 +13,8 @@ import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
|
|
|
|
|
|
|
|
|
public class FileLibrayFormPlugin extends ExecutePluginParent { |
|
|
|
public class FileLibrayFormPlugin extends ExecutePluginParent { |
|
|
|
|
|
|
|
|
|
|
|
@ -86,22 +88,49 @@ public class FileLibrayFormPlugin extends ExecutePluginParent { |
|
|
|
this.setWidgetAttribute("fileName", AttributeEnum.DISABLED, true); |
|
|
|
this.setWidgetAttribute("fileName", AttributeEnum.DISABLED, true); |
|
|
|
} |
|
|
|
} |
|
|
|
JSONObject item = fileJson.getJSONObject(0); |
|
|
|
JSONObject item = fileJson.getJSONObject(0); |
|
|
|
String realName = item.getString("fileRealName"); |
|
|
|
String realName = item.getString("filename"); |
|
|
|
String fullFilePath = item.getString("uploadPath") + "/" + item.getString("filename"); |
|
|
|
String fullFilePath = item.getString("baseUrl")+"/"+item.getString("uploadPath") + "/" + item.getString("filename"); |
|
|
|
|
|
|
|
fullFilePath = fixOssUrl(fullFilePath); |
|
|
|
File file = new File(fullFilePath); |
|
|
|
if (fullFilePath != null) { |
|
|
|
|
|
|
|
// 判断是否为网络地址(http/https 开头)
|
|
|
|
if (file.exists() && file.isFile()) { |
|
|
|
if (fullFilePath.startsWith("http://") || fullFilePath.startsWith("https://")) { |
|
|
|
// 获取文件大小(字节)
|
|
|
|
// 网络地址,使用 URL 连接获取信息
|
|
|
|
long fileSize = file.length(); |
|
|
|
try { |
|
|
|
|
|
|
|
URL url = new URL(fullFilePath); |
|
|
|
// 获取文件类型(根据文件名后缀)
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
|
|
|
String fileType = getFileType(realName); |
|
|
|
connection.setRequestMethod("HEAD"); |
|
|
|
this.setWidgetAttribute("fileType", AttributeEnum.VALUE_CHANGE, fileType); |
|
|
|
connection.connect(); |
|
|
|
this.setWidgetAttribute("fileSize", AttributeEnum.VALUE_CHANGE, formatFileSize(fileSize)); |
|
|
|
|
|
|
|
|
|
|
|
// 获取文件大小
|
|
|
|
} else { |
|
|
|
long fileSize = connection.getContentLengthLong(); |
|
|
|
System.out.println("文件不存在:" + fullFilePath); |
|
|
|
|
|
|
|
|
|
|
|
// 获取文件类型(从 URL 中提取扩展名)
|
|
|
|
|
|
|
|
String fileType = getFileType(realName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fileSize > 0) { |
|
|
|
|
|
|
|
this.setWidgetAttribute("fileType", AttributeEnum.VALUE_CHANGE, fileType); |
|
|
|
|
|
|
|
this.setWidgetAttribute("fileSize", AttributeEnum.VALUE_CHANGE, formatFileSize(fileSize)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// HEAD 请求无法获取大小时,改用 GET 流读取(不推荐用于大文件)
|
|
|
|
|
|
|
|
System.out.println("无法获取文件大小:" + fullFilePath); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connection.disconnect(); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
System.out.println("获取网络文件信息失败:" + e.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// 本地文件路径
|
|
|
|
|
|
|
|
File file = new File(fullFilePath); |
|
|
|
|
|
|
|
if (file.exists() && file.isFile()) { |
|
|
|
|
|
|
|
long fileSize = file.length(); |
|
|
|
|
|
|
|
String fileType = getFileType(realName); |
|
|
|
|
|
|
|
this.setWidgetAttribute("fileType", AttributeEnum.VALUE_CHANGE, fileType); |
|
|
|
|
|
|
|
this.setWidgetAttribute("fileSize", AttributeEnum.VALUE_CHANGE, formatFileSize(fileSize)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
System.out.println("文件不存在:" + fullFilePath); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
@ -113,5 +142,25 @@ public class FileLibrayFormPlugin extends ExecutePluginParent { |
|
|
|
return "unknown"; |
|
|
|
return "unknown"; |
|
|
|
} |
|
|
|
} |
|
|
|
return fileName.substring(fileName.lastIndexOf(".") + 1); |
|
|
|
return fileName.substring(fileName.lastIndexOf(".") + 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 修复OSS文件URL |
|
|
|
|
|
|
|
* @param invalidUrl 错误的URL: https://yy-test12.https://oss-cn-beijing.aliyuncs.com/attachment/...
|
|
|
|
|
|
|
|
* @return 正确的URL: https://yy-test12.oss-cn-beijing.aliyuncs.com/attachment/...
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String fixOssUrl(String invalidUrl) { |
|
|
|
|
|
|
|
if (invalidUrl == null || invalidUrl.isEmpty()) { |
|
|
|
|
|
|
|
return invalidUrl; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查找第一个 "https://" 后的内容
|
|
|
|
|
|
|
|
String domain = invalidUrl.replaceFirst("^https://", ""); |
|
|
|
|
|
|
|
// 将 "yy-test12.https://oss-cn-beijing.aliyuncs.com"
|
|
|
|
|
|
|
|
// 替换为 "yy-test12.oss-cn-beijing.aliyuncs.com"
|
|
|
|
|
|
|
|
String fixedUrl = domain.replace(".https://", "."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重新拼接 https://
|
|
|
|
|
|
|
|
return "https://" + fixedUrl; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|