|
|
|
@ -6,7 +6,6 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
|
import java.net.MalformedURLException; |
|
|
|
|
import java.net.URL; |
|
|
|
|
import java.net.URLDecoder; |
|
|
|
|
import java.util.Base64; |
|
|
|
@ -17,7 +16,7 @@ import java.util.Base64;
|
|
|
|
|
*/ |
|
|
|
|
@Slf4j |
|
|
|
|
public class FileUtil { |
|
|
|
|
public static byte[] getFileBytes(String filePath) { |
|
|
|
|
public static byte[] getFileBytes(String filePath) throws IOException { |
|
|
|
|
File file = new File(filePath); |
|
|
|
|
FileInputStream fileInputStream = null; |
|
|
|
|
byte[] bytes = null; |
|
|
|
@ -25,21 +24,15 @@ public class FileUtil {
|
|
|
|
|
fileInputStream = new FileInputStream(file); |
|
|
|
|
bytes = new byte[(int) file.length()]; |
|
|
|
|
fileInputStream.read(bytes); |
|
|
|
|
return bytes; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("读取文件出错", e); |
|
|
|
|
} finally { |
|
|
|
|
if (null != fileInputStream) { |
|
|
|
|
try { |
|
|
|
|
fileInputStream.close(); |
|
|
|
|
} catch (IOException ignore) { |
|
|
|
|
} |
|
|
|
|
fileInputStream.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return bytes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static byte[] getUrlByte(URL url) { |
|
|
|
|
public static byte[] getUrlByte(URL url) throws IOException { |
|
|
|
|
InputStream inputStream = null; |
|
|
|
|
ByteArrayOutputStream outputStream = null; |
|
|
|
|
byte[] fileBytes = null; |
|
|
|
@ -52,30 +45,19 @@ public class FileUtil {
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
|
|
if (null != inputStream) { |
|
|
|
|
inputStream.close(); |
|
|
|
|
} |
|
|
|
|
if (null != outputStream) { |
|
|
|
|
outputStream.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
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 byte[] getUrlByte(String urlString) throws IOException { |
|
|
|
|
return getUrlByte(new URL(urlString)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getContentTypeFromUrl(String url) { |
|
|
|
@ -113,16 +95,12 @@ public class FileUtil {
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getPureUrl(String url) { |
|
|
|
|
public static String getPureUrl(String url) throws UnsupportedEncodingException { |
|
|
|
|
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失败"); |
|
|
|
|
result = URLDecoder.decode(result, "utf-8"); |
|
|
|
|
int index = result.lastIndexOf("http"); |
|
|
|
|
if (index > 0) { |
|
|
|
|
result = result.substring(index); |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
@ -144,23 +122,19 @@ public class FileUtil {
|
|
|
|
|
return url; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static MultipartFile getMultipartFileFromUrl(String urlStr) { |
|
|
|
|
public static MultipartFile getMultipartFileFromUrl(String urlStr) throws IOException { |
|
|
|
|
String pureUrl = getPureUrl(urlStr); |
|
|
|
|
return getMultipartFileFromPureUrl(pureUrl); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static MultipartFile getMultipartFileFromPureUrl(String pureUrlStr) { |
|
|
|
|
public static MultipartFile getMultipartFileFromPureUrl(String pureUrlStr) throws IOException { |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
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); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -168,7 +142,7 @@ public class FileUtil {
|
|
|
|
|
return Base64.getEncoder().encodeToString(bytes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getBase64(String filePath) { |
|
|
|
|
public static String getBase64(String filePath) throws IOException { |
|
|
|
|
return getBase64(getFileBytes(filePath)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|