|
|
@ -11,13 +11,20 @@ import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
|
|
import org.apache.http.ssl.SSLContextBuilder; |
|
|
|
import org.apache.http.ssl.SSLContextBuilder; |
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
|
|
|
|
import org.springblade.core.tool.utils.StringUtil; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection; |
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
import java.io.*; |
|
|
|
import java.io.*; |
|
|
|
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLDecoder; |
|
|
|
import java.net.URLConnection; |
|
|
|
|
|
|
|
import java.security.KeyManagementException; |
|
|
|
|
|
|
|
import java.security.KeyStoreException; |
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
import java.util.Base64; |
|
|
|
import java.util.Base64; |
|
|
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @Author: ypj |
|
|
|
* @Author: ypj |
|
|
@ -43,12 +50,16 @@ public class FileUtil { |
|
|
|
return bytes; |
|
|
|
return bytes; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static byte[] getUrlByte(URL url) throws IOException { |
|
|
|
public static byte[] getUrlByte(URL url) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { |
|
|
|
InputStream inputStream = null; |
|
|
|
URLConnection conn = getUrlConnection(url); |
|
|
|
|
|
|
|
conn.connect(); |
|
|
|
|
|
|
|
return getUrlByte(conn.getInputStream()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[] getUrlByte(InputStream inputStream) throws IOException { |
|
|
|
ByteArrayOutputStream outputStream = null; |
|
|
|
ByteArrayOutputStream outputStream = null; |
|
|
|
byte[] fileBytes = null; |
|
|
|
byte[] fileBytes = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
inputStream = url.openStream(); |
|
|
|
|
|
|
|
outputStream = new ByteArrayOutputStream(); |
|
|
|
outputStream = new ByteArrayOutputStream(); |
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
int bytesRead; |
|
|
|
int bytesRead; |
|
|
@ -67,80 +78,46 @@ public class FileUtil { |
|
|
|
return fileBytes; |
|
|
|
return fileBytes; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static byte[] getUrlByte(String urlString) throws IOException { |
|
|
|
public static byte[] getUrlByte(String urlString) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { |
|
|
|
return getUrlByte(new URL(urlString)); |
|
|
|
return getUrlByte(new URL(urlString)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getContentTypeFromUrl(String url) { |
|
|
|
public static String getUrlBase64(String urlString) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException { |
|
|
|
String result = ""; |
|
|
|
return getBase64(getUrlByte(urlString)); |
|
|
|
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) throws UnsupportedEncodingException { |
|
|
|
public static URLConnection getUrlConnection(URL url) throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { |
|
|
|
String result = removeUrlParam(url); |
|
|
|
URLConnection conn = url.openConnection(); |
|
|
|
result = URLDecoder.decode(result, "utf-8"); |
|
|
|
if (url.getProtocol().equalsIgnoreCase("https")) { |
|
|
|
int index = result.lastIndexOf("http"); |
|
|
|
SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(null, (x509Certificate, s) -> true).build(); |
|
|
|
if (index > 0) { |
|
|
|
((HttpsURLConnection) conn).setSSLSocketFactory(sslContext.getSocketFactory()); |
|
|
|
result = result.substring(index); |
|
|
|
((HttpsURLConnection) conn).setHostnameVerifier(NoopHostnameVerifier.INSTANCE); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
((HttpURLConnection) conn).setRequestMethod("GET"); |
|
|
|
|
|
|
|
conn.setRequestProperty("User-Agent", "Mozilla/5.0"); |
|
|
|
|
|
|
|
conn.setRequestProperty("Accept", "*/*"); |
|
|
|
|
|
|
|
return conn; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getOriginalFilename(String path) { |
|
|
|
public static URLConnection getUrlConnection(String urlString) throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { |
|
|
|
String result = path; |
|
|
|
URL url = new URL(urlString); |
|
|
|
int index = path.lastIndexOf("/"); |
|
|
|
return getUrlConnection(url); |
|
|
|
if (index > 0) { |
|
|
|
|
|
|
|
result = path.substring(index + 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String removeUrlParam(String url) { |
|
|
|
public static ByteArrayMultipartFile getMultipartFileFromUrl(String urlStr, String fileName) { |
|
|
|
int index = url.indexOf("?"); |
|
|
|
|
|
|
|
if (index > 0) { |
|
|
|
|
|
|
|
return url.substring(0, index); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return url; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static ByteArrayMultipartFile getMultipartFileFromUrl(String urlStr) throws IOException { |
|
|
|
|
|
|
|
ByteArrayMultipartFile result = null; |
|
|
|
ByteArrayMultipartFile result = null; |
|
|
|
URL url = new URL(urlStr); |
|
|
|
try { |
|
|
|
byte[] bytes = getUrlByte(url); |
|
|
|
URL url = new URL(urlStr); |
|
|
|
String name = url.getPath(); |
|
|
|
URLConnection conn = getUrlConnection(url); |
|
|
|
String originalName = getOriginalFilename(name); |
|
|
|
conn.connect(); |
|
|
|
String contentType = getContentTypeFromUrl(originalName); |
|
|
|
byte[] bytes = getUrlByte(conn.getInputStream()); |
|
|
|
result = new ByteArrayMultipartFile(name, originalName, contentType, bytes); |
|
|
|
String name = url.getPath(); |
|
|
|
|
|
|
|
String originalName = StringUtil.isBlank(fileName) ? UUID.randomUUID().toString() : fileName; |
|
|
|
|
|
|
|
String contentType = conn.getContentType(); |
|
|
|
|
|
|
|
result = new ByteArrayMultipartFile(name, originalName, contentType, bytes); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error("获取传输文件失败", e); |
|
|
|
|
|
|
|
} |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -148,11 +125,11 @@ public class FileUtil { |
|
|
|
return Base64.getEncoder().encodeToString(bytes); |
|
|
|
return Base64.getEncoder().encodeToString(bytes); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getBase64(String filePath) throws IOException { |
|
|
|
public static String getFileBase64(String filePath) throws IOException { |
|
|
|
return getBase64(getFileBytes(filePath)); |
|
|
|
return getBase64(getFileBytes(filePath)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getBase64(URL url) throws IOException { |
|
|
|
public static String getUrlBase64(URL url) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { |
|
|
|
return getBase64(getUrlByte(url)); |
|
|
|
return getBase64(getUrlByte(url)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|