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