|  |  | @ -1,10 +1,14 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | package com.hnac.hzims.common.utils; |  |  |  | package com.hnac.hzims.common.utils; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import lombok.NonNull; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import lombok.Value; | 
			
		
	
		
		
			
				
					
					|  |  |  | import lombok.extern.slf4j.Slf4j; |  |  |  | import lombok.extern.slf4j.Slf4j; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import org.springframework.web.multipart.MultipartFile; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.File; |  |  |  | import java.io.*; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.FileInputStream; |  |  |  | import java.net.MalformedURLException; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.IOException; |  |  |  | import java.net.URL; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import java.net.URLDecoder; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.util.Base64; |  |  |  | import java.util.Base64; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | /** |  |  |  | /** | 
			
		
	
	
		
		
			
				
					|  |  | @ -35,6 +39,131 @@ public class FileUtil { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return bytes; |  |  |  |         return bytes; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static byte[] getUrlByte(URL url) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         InputStream inputStream = null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         ByteArrayOutputStream outputStream = null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         byte[] fileBytes = null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         try { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             inputStream = url.openStream(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             outputStream = new ByteArrayOutputStream(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             byte[] buffer = new byte[4096]; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             int bytesRead; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             while ((bytesRead = inputStream.read(buffer)) != -1) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 outputStream.write(buffer, 0, bytesRead); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             fileBytes = outputStream.toByteArray(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } catch (Exception e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             log.error("获取url文件失败", e); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } finally { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             try { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (null != inputStream) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     inputStream.close(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (null != outputStream) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     outputStream.close(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } catch (Exception ignore) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return fileBytes; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static byte[] getUrlByte(String urlString) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         try { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return getUrlByte(new URL(urlString)); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } catch (MalformedURLException e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             log.error("获取url文件失败", e); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static String getContentTypeFromUrl(String url) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String result = ""; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String fileType = url.toLowerCase(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         if (fileType.indexOf(".png") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "image/png"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".gif") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "image/gif"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".jpg") >= 0 || fileType.indexOf(".jpeg") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "image/jpeg"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".svg") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "image/svg+xml"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".doc") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "application/msword"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".xls") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "application/x-excel"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".zip") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "application/zip"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".pdf") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "application/pdf"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".mpeg") >= 0) { //MP3
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "audio/mpeg"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".mp4") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "video/mp4"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".plain") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "text/plain"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".html") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "text/html"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else if (fileType.indexOf(".json") >= 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "application/json"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } else { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = "application/octet-stream"; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return result; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static String getPureUrl(String url) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String result = removeUrlParam(url); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         try { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = URLDecoder.decode(result, "utf-8"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             int index = result.lastIndexOf("http"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (index > 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 result = result.substring(index); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } catch (UnsupportedEncodingException e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             log.error("解码url失败"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return result; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static String getOriginalFilename(String path) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String result = path; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         int index = path.lastIndexOf("/"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         if (index > 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = path.substring(index + 1); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return result; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static String removeUrlParam(String url) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         int index = url.indexOf("&"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         if (index > 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return url.substring(0, index); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return url; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static MultipartFile getMultipartFileFromUrl(String urlStr) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String pureUrl = getPureUrl(urlStr); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return getMultipartFileFromPureUrl(pureUrl); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static MultipartFile getMultipartFileFromPureUrl(String pureUrlStr) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         MultipartFile result = null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         try { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             URL url = new URL(pureUrlStr); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             byte[] bytes = getUrlByte(url); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             String name = url.getPath(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             String originalName = getOriginalFilename(name); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             String contentType = getContentTypeFromUrl(originalName); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             result = new ByteArrayMultipartFile(name, originalName, contentType, bytes); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } catch (Exception e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             log.error("获取url文件失败", e); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return result; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     public static String getBase64(byte[] bytes) { |  |  |  |     public static String getBase64(byte[] bytes) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return Base64.getEncoder().encodeToString(bytes); |  |  |  |         return Base64.getEncoder().encodeToString(bytes); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
	
		
		
			
				
					|  |  | @ -42,4 +171,44 @@ public class FileUtil { | 
			
		
	
		
		
			
				
					
					|  |  |  |     public static String getBase64(String filePath) { |  |  |  |     public static String getBase64(String filePath) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return getBase64(getFileBytes(filePath)); |  |  |  |         return getBase64(getFileBytes(filePath)); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     @Value | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static class ByteArrayMultipartFile implements MultipartFile { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String name; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String originalFilename; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String contentType; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         @NonNull | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         byte[] bytes; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         @Override | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         public boolean isEmpty() { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return bytes.length == 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         @Override | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         public long getSize() { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return bytes.length; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         @Override | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         public InputStream getInputStream() { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return new ByteArrayInputStream(bytes); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         @Override | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         public void transferTo(File destination) throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             OutputStream outputStream = null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             try { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 outputStream = new FileOutputStream(destination); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 outputStream.write(bytes); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } finally { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (outputStream != null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     outputStream.close(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |