package com.boying.util;
|
|
import com.boying.common.SystemConfigProperties;
|
import com.boying.entity.FileInfo;
|
import com.boying.entity.OrderRecord;
|
import com.boying.service.FileInfoService;
|
import com.boying.service.OrderRecordService;
|
import lombok.AllArgsConstructor;
|
import org.apache.commons.codec.binary.Base64;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
import sun.misc.BASE64Encoder;
|
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Objects;
|
import java.util.UUID;
|
|
@Component
|
@AllArgsConstructor
|
public class FileUtil {
|
private static SystemConfigProperties systemConfigProperties;
|
private static FileInfoService fileInfoService;
|
private static OrderRecordService orderRecordService;
|
|
public static String saveFile(MultipartFile multipartFile){
|
String filePath = systemConfigProperties.getFilePath();
|
File file = null;
|
if(multipartFile.getSize() <= 0){
|
return null;
|
}else {
|
//文件原名称
|
String originalFilename = multipartFile.getOriginalFilename();
|
//文件格式
|
String substring = Objects.requireNonNull(originalFilename).substring(originalFilename.lastIndexOf("."));
|
//文件id
|
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
|
//文件路径
|
file=new File(filePath+File.separator+uuid+substring);
|
String absPath = null;
|
try {
|
absPath = file.getCanonicalPath();
|
String substring1 = absPath.substring(0, absPath.lastIndexOf(File.separator));
|
File file1 = new File(substring1);
|
if(!file1.exists()){
|
file1.mkdirs();
|
}
|
//写入
|
InputStream inputStream = multipartFile.getInputStream();
|
inputStreamToFile(inputStream,file);
|
inputStream.close();
|
}catch (IOException e){
|
e.printStackTrace();
|
}
|
return absPath;
|
}
|
|
}
|
|
public static void inputStreamToFile(InputStream inputStream,File file){
|
try {
|
OutputStream os = new FileOutputStream(file);
|
int bytesRead = 0;
|
byte[] buffer = new byte[8192];
|
while ((bytesRead = inputStream.read(buffer,0,8192)) != -1){
|
os.write(buffer,0,bytesRead);
|
}
|
os.close();
|
inputStream.close();
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
|
public static FileInfo generateBase64StringToFile(String fileString,int t,int type){
|
String lastname= null;
|
//文件id
|
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
|
if(t == 1){
|
lastname=".png";
|
}else {
|
lastname=".pdf";
|
}
|
String filePath = "D:\\FFproject\\file\\";
|
if(fileString == null){
|
return null;
|
}else {
|
try {
|
byte[] b = Base64.decodeBase64(fileString);
|
for (int i = 0; i < b.length; ++i) {
|
if(b[i]<0){
|
b[i]+=256;
|
}
|
}
|
OutputStream out = new FileOutputStream(filePath+uuid+lastname);
|
out.write(b);
|
FileInfo fileInfo = new FileInfo();
|
fileInfo.setFileBaseDir(filePath);
|
fileInfo.setFileName(uuid);
|
fileInfo.setFileLast(lastname);
|
fileInfo.setType(type);
|
out.flush();
|
out.close();
|
return fileInfo;
|
}catch (Exception e){
|
e.printStackTrace();
|
return null;
|
}
|
|
}
|
}
|
|
public static String netSourceToBase64(String srcUrl,String requestMethod) {
|
ByteArrayOutputStream outPut = new ByteArrayOutputStream();
|
byte[] data = new byte[1024 * 8];
|
try {
|
// 创建URL
|
URL url = new URL(srcUrl);
|
// 创建链接
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
conn.setRequestMethod(requestMethod);
|
conn.setConnectTimeout(20000);
|
|
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
//连接失败/链接失效/文件不存在
|
return null;
|
}
|
InputStream inStream = conn.getInputStream();
|
int len = -1;
|
while (-1 != (len = inStream.read(data))) {
|
outPut.write(data, 0, len);
|
}
|
inStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
// 对字节数组Base64编码
|
BASE64Encoder encoder = new BASE64Encoder();
|
return encoder.encode(outPut.toByteArray());
|
}
|
|
public static void main(String[] args) {
|
String get = netSourceToBase64("http://36.134.129.218:9999/admin/sys-file/platform/20230815083801.png", "GET");
|
System.out.println(get);
|
}
|
|
}
|