1212import java .io .IOException ;
1313import java .io .InputStream ;
1414import java .io .UnsupportedEncodingException ;
15- import java .net .*;
15+ import java .net .HttpURLConnection ;
16+ import java .net .URI ;
17+ import java .net .URISyntaxException ;
18+ import java .net .URL ;
1619import java .util .Arrays ;
20+ import java .util .Objects ;
1721
1822public class FileUtil {
1923
20- private static final Logger logger = LoggerFactory .getLogger (FileUtil . class );
24+ private static final Logger logger = LoggerFactory .getLogger ("LCAP_CUSTOMIZE_LOGGER" );
2125
2226 public static InputStream getFileInputStream (String urlStr ) throws IOException {
2327 URL url = new URL (getTrueUrl (urlStr ));
@@ -26,6 +30,7 @@ public static InputStream getFileInputStream(String urlStr) throws IOException {
2630 connection .setRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36" );
2731 return url .openStream ();
2832 }
33+
2934 // 新增方法:检测 URL 是否已经编码
3035 private static boolean isUrlEncoded (String url ) {
3136 try {
@@ -35,6 +40,7 @@ private static boolean isUrlEncoded(String url) {
3540 return false ;
3641 }
3742 }
43+
3844 public static String getTrueUrl (String urlStr ) throws UnsupportedEncodingException {
3945 URI uri = null ;
4046 try {
@@ -56,19 +62,30 @@ public static String getTrueUrl(String urlStr) throws UnsupportedEncodingExcepti
5662 String fileNameEncoded = java .net .URLEncoder .encode (fileName , "UTF-8" );
5763 urlStr = urlStr .replace (fileName , fileNameEncoded );
5864 }
59- if (!urlStr .startsWith ("http" ) && urlStr .startsWith ("/upload" )) {
60- HttpServletRequest request = ((ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ()).getRequest ();
61- int port = request .getLocalPort ();
62- return "http://127.0.0.1:" + port + urlStr ;
65+ try {
66+ if (!urlStr .startsWith ("http" ) && urlStr .startsWith ("/upload" )) {
67+ HttpServletRequest request = ((ServletRequestAttributes ) Objects .requireNonNull (RequestContextHolder .getRequestAttributes ())).getRequest ();
68+ int port = request .getLocalPort ();
69+ return "http://127.0.0.1:" + port + urlStr ;
70+ }
71+ } catch (Exception e ) {
72+ logger .error ("urlStr转本地失败" , e );
6373 }
6474 return urlStr ;
6575 }
6676
6777 public static UploadResponseDTO uploadStream (InputStream inputStream , String fileName ) throws IOException {
68- HttpServletRequest httpServletRequest = ((ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ()).getRequest ();
69- int port = httpServletRequest .getLocalPort ();
70-
78+ int port ;
79+ try {
80+ HttpServletRequest httpServletRequest = ((ServletRequestAttributes ) Objects .requireNonNull (RequestContextHolder .getRequestAttributes ())).getRequest ();
81+ port = httpServletRequest .getLocalPort ();
82+ } catch (Exception e ) {
83+ logger .error ("获取本地port失败,默认获取8080" , e );
84+ //兜底方案
85+ port = 8080 ;
86+ }
7187 String uploadUrl = "http://127.0.0.1:" + port + "/gateway/lowcode/api/v1/app/upload" ;
88+
7289 OkHttpClient client = new OkHttpClient ();
7390
7491 byte [] fileBytes ;
@@ -97,7 +114,7 @@ public static UploadResponseDTO uploadStream(InputStream inputStream, String fil
97114 if (response .isSuccessful ()) {
98115 return JsonUtil .fromJson (response .body ().string (), UploadResponseDTO .class );
99116 }
100- logger .error (String .format ("文件上传失败,%s" ,response ));
101- throw new RuntimeException (String .format ("文件上传失败,%s" ,response ));
117+ logger .error (String .format ("文件上传失败,%s" , response ));
118+ throw new RuntimeException (String .format ("文件上传失败,%s" , response ));
102119 }
103120}
0 commit comments