@ -13,6 +13,8 @@ import com.alibaba.fastjson.JSONArray;
@@ -13,6 +13,8 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject ;
import java.io.File ;
import java.net.HttpURLConnection ;
import java.net.URL ;
public class FileLibrayFormPlugin extends ExecutePluginParent {
@ -86,23 +88,50 @@ public class FileLibrayFormPlugin extends ExecutePluginParent {
@@ -86,23 +88,50 @@ public class FileLibrayFormPlugin extends ExecutePluginParent {
this . setWidgetAttribute ( "fileName" , AttributeEnum . DISABLED , true ) ;
}
JSONObject item = fileJson . getJSONObject ( 0 ) ;
String realName = item . getString ( "fileRealName" ) ;
String fullFilePath = item . getString ( "uploadPath" ) + "/" + item . getString ( "filename" ) ;
String realName = item . getString ( "filename" ) ;
String fullFilePath = item . getString ( "baseUrl" ) + "/" + item . getString ( "uploadPath" ) + "/" + item . getString ( "filename" ) ;
fullFilePath = fixOssUrl ( fullFilePath ) ;
if ( fullFilePath ! = null ) {
// 判断是否为网络地址(http/https 开头)
if ( fullFilePath . startsWith ( "http://" ) | | fullFilePath . startsWith ( "https://" ) ) {
// 网络地址,使用 URL 连接获取信息
try {
URL url = new URL ( fullFilePath ) ;
HttpURLConnection connection = ( HttpURLConnection ) url . openConnection ( ) ;
connection . setRequestMethod ( "HEAD" ) ;
connection . connect ( ) ;
File file = new File ( fullFilePath ) ;
// 获取文件大小
long fileSize = connection . getContentLengthLong ( ) ;
// 获取文件类型(从 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 {
@@ -113,5 +142,25 @@ public class FileLibrayFormPlugin extends ExecutePluginParent {
return "unknown" ;
}
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 ;
}
}