java ftp+ssl上传文件
参考文章:
ftps 上传:https://blog.csdn.net/Luo_jin_yuan/article/details/83052007#java_4
乱码处理:https://blog.csdn.net/u010166206/article/details/47000729/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
public class FtpsUtil {
/** 本地字符编码 */
private static String LOCAL_CHARSET = "GBK";
// FTP协议里面,规定文件名编码为iso-8859-1
private static String SERVER_CHARSET = "ISO-8859-1";
public static void putFile(String host, int port, String username, String password, List<byte[]> file,
List<String> remoteFilename) throws NoSuchAlgorithmException {
try {
System.out.println("-------------------------------");
FTPSClient ftpClient = new FTPSClient();
// Connect to host
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Login
if (ftpClient.login(username, password)) {
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
"OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
LOCAL_CHARSET = "UTF-8";
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding(LOCAL_CHARSET);
// Set protection buffer size
ftpClient.execPBSZ(0);
// Set data channel protection to private
ftpClient.execPROT("P");
// Enter local passive mode
ftpClient.enterLocalPassiveMode();
// Store file on host
for (int i = 0; i < file.size(); i++) {
String fileName = remoteFilename.get(i);
// 上传文件名编码转换
fileName = new String(remoteFilename.get(i).getBytes(LOCAL_CHARSET),SERVER_CHARSET);
InputStream is = new ByteArrayInputStream(file.get(i));
if (ftpClient.storeFile(fileName, is)) {
System.out.println(username+","+remoteFilename.get(i));
} else {
System.out.println("Could not store file");
}
is.close();
}
// Logout
ftpClient.logout();
} else {
System.out.println("FTP login failed");
}
// Disconnect
ftpClient.disconnect();
} else {
System.out.println("FTP connect to host failed");
}
} catch (IOException ioe) {
System.out.println("FTP client received network error");
}
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String path = "D://test.txt";
String fileName = "test.txt";
File file = new File(path);
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
byte[] data = bos.toByteArray();
bos.close();
List<byte[]> list1 = new ArrayList<byte[]>();
List<String> list2 = new ArrayList<String>();
list1.add(data);
list2.add(fileName);
FtpsUtil.putFile("xxx.xxx.xx.xx", xx, "username", "password", list1, list2);
//FtpUtil.putFile(host, port, username, password, bytes, fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我这里需要做的是批量文件上传同一用户,所以自己把传进来的文件参数返回类型改成了list避免同一用户多次连接服务器。
期间遇到乱码问题,从网上搜到解决方案。
还发现 上传excel文件至ftp服务器之后,从ftp上down下来的excel文件打开会提示出错,这家伙让我研究了两个小时。。幸好也从网上找到了问题解决,需要定义文件上传的类型格式
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);