mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-12 03:09:48 +08:00
update上传固件
This commit is contained in:
@@ -18,6 +18,7 @@ import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -47,11 +48,12 @@ public class FtpUtils {
|
||||
public static String upLoad(String baseDir, MultipartFile file) throws Exception {
|
||||
FTPClient ftp = new FTPClient();
|
||||
baseDir = "/";
|
||||
String fileName = null;
|
||||
try {
|
||||
/** 1. 检查文件大小和扩展名是否符合要求*/
|
||||
assertFile(file);
|
||||
/** 2. 产生新的文件名,目的使得文件名统一为英文字符加数字;fileName包含文件后缀名*/
|
||||
String fileName = reBuildFileName(file);
|
||||
fileName = reBuildFileName(file);
|
||||
/** 3. 连接ftp服务器*/
|
||||
String ip = IpUtils.getIpFormDomainName(ftpConfig.getIp());
|
||||
ftp.connect(ip, ftpConfig.getPort());
|
||||
@@ -61,6 +63,7 @@ public class FtpUtils {
|
||||
} else {
|
||||
log.info("FTP登录失败");
|
||||
}
|
||||
ftp.setRemoteVerificationEnabled(false);
|
||||
ftp.enterLocalPassiveMode(); // 设置为被动模式
|
||||
log.info("ftp连接信息, ip:{}, port:{}, 连接信息:{}", ip, ftpConfig.getPort(), ftp.getStatus());
|
||||
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
|
||||
@@ -90,11 +93,23 @@ public class FtpUtils {
|
||||
// 设置缓冲区大小
|
||||
ftp.setBufferSize(3072);
|
||||
// 上传文件
|
||||
ftp.storeFile(fileName, file.getInputStream());
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
boolean uploaded = ftp.storeFile(fileName, inputStream);
|
||||
if (!uploaded) {
|
||||
throw new IOException("FTP上传失败,响应信息:" + ftp.getReplyString());
|
||||
}
|
||||
}
|
||||
// 登出服务器
|
||||
ftp.logout();
|
||||
return baseDir + "/" + fileName;
|
||||
} catch (Exception e) {
|
||||
if (StringUtils.isNotEmpty(fileName) && ftp.isConnected()) {
|
||||
try {
|
||||
ftp.deleteFile(fileName);
|
||||
} catch (IOException deleteException) {
|
||||
log.warn("删除FTP上传失败残留文件异常, fileName:{}", fileName, deleteException);
|
||||
}
|
||||
}
|
||||
throw new Exception(e.getMessage(), e);
|
||||
} finally {
|
||||
/** 关闭*/
|
||||
|
||||
Reference in New Issue
Block a user