parent
0a56ba37cc
commit
e66903730b
@ -0,0 +1,95 @@ |
||||
package com.dxhy.order.consumer.modules.bespoke.config.datasource; |
||||
|
||||
import cn.hutool.core.codec.Base64; |
||||
import cn.hutool.core.io.FileUtil; |
||||
import cn.hutool.core.io.IoUtil; |
||||
import com.dxhy.management.common.utils.Base64Encoding; |
||||
import org.apache.commons.net.ftp.FTPClient; |
||||
import org.apache.commons.net.ftp.FTPFile; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.IOException; |
||||
import java.nio.charset.Charset; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
@Component |
||||
@ConfigurationProperties(prefix = "luoxin") |
||||
public class LuoxinBack { |
||||
|
||||
private static Map<String,String> back = new HashMap<>(); |
||||
|
||||
public String getWrite(String nsrsbh){ |
||||
return back.get(nsrsbh); |
||||
} |
||||
|
||||
public void setBack(Map<String,String> back){ |
||||
this.back = back; |
||||
} |
||||
|
||||
|
||||
public static void main(String[] args) { |
||||
|
||||
String server = "192.168.99.218"; // FTP服务器地址
|
||||
int port = 21; // FTP服务器端口,默认为21
|
||||
String user = "ftpadmin"; // FTP登录用户名
|
||||
String pass = "Lx8b0a6#"; // FTP登录密码
|
||||
String fileName = "24372000000156332224-88.50.pdf"; |
||||
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), Charset.forName("GBK")); |
||||
System.out.println(fileName); |
||||
String path = "/test"; |
||||
String fullPath = path + "/" + fileName; |
||||
FTPClient ftpClient = new FTPClient(); |
||||
try { |
||||
ftpClient.connect(server, port); |
||||
ftpClient.login(user, pass); |
||||
String s = FileUtil.readString("D:\\invoice.txt", "UTF-8"); |
||||
byte[] decode = Base64Encoding.decode(s); |
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); |
||||
ByteArrayInputStream inputStream = IoUtil.toStream(decode); |
||||
boolean dirExists = ftpClient.changeWorkingDirectory(path); |
||||
if (!dirExists) { |
||||
// 递归创建目录
|
||||
String[] dirs = path.split("/"); |
||||
StringBuilder currentDir = new StringBuilder(); |
||||
for (String dir : dirs) { |
||||
if (!dir.isEmpty()) { |
||||
currentDir.append('/').append(dir); |
||||
if (!ftpClient.changeWorkingDirectory(currentDir.toString())) { |
||||
if (!ftpClient.makeDirectory(currentDir.toString())) { |
||||
throw new IOException("Failed to create directory: " + currentDir.toString()); |
||||
} |
||||
ftpClient.changeWorkingDirectory(currentDir.toString()); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
FTPFile[] files = ftpClient.listFiles(path.substring(0, path.lastIndexOf('/'))); |
||||
boolean fileExists = false; |
||||
for (FTPFile file : files) { |
||||
if (file.getName().equals(fileName)) { |
||||
fileExists = true; |
||||
break; |
||||
} |
||||
} |
||||
if (fileExists) { |
||||
// 删除文件
|
||||
boolean deleted = ftpClient.deleteFile(fullPath); |
||||
if (deleted) { |
||||
ftpClient.storeFile(fileName, inputStream); |
||||
} else { |
||||
} |
||||
} else { |
||||
// 直接上传文件
|
||||
ftpClient.storeFile(fileName, inputStream); |
||||
} |
||||
}catch (Exception e){ |
||||
|
||||
}finally { |
||||
|
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue