更新:解决了
我FTPClient.setFileType() 在登录之前打电话,导致FTP服务器使用默认模式(ASCII),无论我将其设置为什么.另一方面,客户端的行为就像文件类型已正确设置一样.BINARY模式现在完全按照需要工作,在所有情况下逐字节传输文件.我所要做的只是在wireshark中进行一些流量嗅探,然后使用netcat模拟FTP命令以查看发生了什么.为什么我两天前没有想到这个??谢谢,大家帮忙!
我有一个xml文件,utf-16编码,我使用apache的commons-net-2.0 java库的FTPClient从FTP站点下载.它提供了支持两种传输模式:ASCII_FILE_TYPE和BINARY_FILE_TYPE,不同之处在于ASCII将合适的地方行分隔符替换行分隔符('\r\n'或只是'\n'-十六进制,0x0d0a或只是0x0a).我的问题是:我有一个测试文件,utf-16编码,包含以下内容:
<?xml version='1.0' encoding='utf-16'?>
<data>
<blah>blah</blah>
</data>
这是十六进制:
0000000: 003c 003f 0078 006d 006c 0020 0076 0065 .<.?.x.m.l. .v.e
0000010: 0072 0073 0069 006f 006e 003d 0027 0031 .r.s.i.o.n.=.'.1
0000020: 002e 0030 0027 0020 0065 006e 0063 006f ...0.'. .e.n.c.o
0000030: 0064 0069 006e 0067 003d 0027 0075 0074 .d.i.n.g.=.'.u.t
0000040: …
我正在尝试做一个非常简单的文件上传.我想要一个可以上传我告诉它的任何文件的Java FTPClient.但pdf总是搞砸了,我的pdf编辑器(Adobe)不会打开它,说有一个I/O错误.
我正在使用以下课程:
import org.apache.commons.net.ftp.FTPClient;
....
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("mydomain.com");
client.login("user", "password");
String filename = "myPDF.pdf";
fis = new FileInputStream(filename);
client.storeFile("temp.pdf", fis);
fis.close();
client.logout();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用,我该如何解决?
我们使用以下Apache Commons Net FTP代码连接到FTP服务器,轮询某些目录以查找文件,如果找到文件,则将它们检索到本地计算机:
try {
logger.trace("Attempting to connect to server...");
// Connect to server
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(20000);
ftpClient.connect("my-server-host-name");
ftpClient.login("myUser", "myPswd");
ftpClient.changeWorkingDirectory("/loadables/");
// Check for failed connection
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
ftpClient.disconnect();
throw new FTPConnectionClosedException("Unable to connect to FTP server.");
}
// Log success msg
logger.trace("...connection was successful.");
// Change to the loadables/ directory where we poll for files
ftpClient.changeWorkingDirectory("/loadables/");
// Indicate we're about to poll
logger.trace("About to check loadables/ for files...");
// Poll for files.
FTPFile[] filesList = …Run Code Online (Sandbox Code Playgroud) 请弄清楚这一点.代码正常运行,没有任何异常.
FTPClient ftp = new FTPClient();
ftp.connect(server);
if(!ftp.login(username, password))
{
ftp.logout();
return false;
}
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return false;
}
InputStream in = new FileInputStream(localfile);
ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE);
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
Store = ftp.storeFile(destinationfile, in);
in.close();
ftp.logout();
ftp.disconnect();
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
return Store;
Run Code Online (Sandbox Code Playgroud)
Buttttttttt
return语句始终返回false,并且文件未上载到服务器上.有人请帮忙.
为了您的信息,1)我在办公室网络.--->我们需要添加任何代理吗?
File file = new File("C:\\Users\\sg0214273\\Desktop\\seagate\\seagate.txt");
FileInputStream input = new FileInputStream(file);
client.setFileType(FTP.BINARY_FILE_TYPE);
if (!client.storeFile(file.getName(), input)) {
System.out.println("upload failed!");
}
reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
System.out.println("upload failed!");
} …Run Code Online (Sandbox Code Playgroud) 环境:我在64位Windows 7上使用Sun Java JDK 1.8.0_60,使用Spring Integration 4.1.6(内部似乎使用Apache Commons Net 3.3进行FTPS访问).
我正在尝试与我们的应用程序集成,从客户端的FTPS服务器自动下载.我已经使用Spring Integration成功完成了使用Spring Integration的SFTP服务器而没有遇到任何问题,但是这是客户第一次要求我们使用FTPS,并且让它连接起来非常令人费解.虽然在我的实际应用程序中,我正在使用XML bean配置Spring Integration,试图了解什么不起作用我正在使用以下测试代码(虽然我在这里匿名实际的主机/用户名/密码):
final DefaultFtpsSessionFactory sessionFactory = new DefaultFtpsSessionFactory();
sessionFactory.setHost("XXXXXXXXX");
sessionFactory.setPort(990);
sessionFactory.setUsername("XXXXXXX");
sessionFactory.setPassword("XXXXXXX");
sessionFactory.setClientMode(2);
sessionFactory.setFileType(2);
sessionFactory.setUseClientMode(true);
sessionFactory.setImplicit(true);
sessionFactory.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
sessionFactory.setProt("P");
sessionFactory.setProtocol("TLSv1.2");
sessionFactory.setProtocols(new String[]{"TLSv1.2"});
sessionFactory.setSessionCreation(true);
sessionFactory.setCipherSuites(new String[]{"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"});
final FtpSession session = sessionFactory.getSession();
//try {
final FTPFile[] ftpFiles = session.list("/");
logger.debug("FtpFiles: {}", (Object[]) ftpFiles);
//} catch (Exception ignored ) {}
session.close();
Run Code Online (Sandbox Code Playgroud)
我正在运行此代码以-Djavax.net.debug=all打印所有TLS调试信息.
与FTPS服务器的主要"控制"连接工作正常,但是当它试图打开列表的数据连接(或我尝试过的任何其他数据连接)时,我得到了一个javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake,由...引起的java.io.EOFException: SSL peer shut down incorrectly.如果我取消注释session.list …
有没有一种有效的方法来检查FTP服务器上是否存在文件?我正在使用Apache Commons Net.我知道我可以使用listNames方法FTPClient来获取特定目录中的所有文件,然后我可以查看此列表以检查给定文件是否存在,但我不认为它是有效的,尤其是当服务器包含大量文件.
我正在使用Apache Commons FTP上传文件.在上传之前,我想检查服务器上是否已存在该文件,并从该服务器备份到同一服务器上的备份目录.
有谁知道如何将文件从FTP服务器复制到同一台服务器上的备份目录?
public static void uploadWithCommonsFTP(File fileToBeUpload){
FTPClient f = new FTPClient();
FTPFile backupDirectory;
try {
f.connect(server.getServer());
f.login(server.getUsername(), server.getPassword());
FTPFile[] directories = f.listDirectories();
FTPFile[] files = f.listFiles();
for(FTPFile file:directories){
if (!file.getName().equalsIgnoreCase("backup")) {
backupDirectory=file;
} else {
f.makeDirectory("backup");
}
}
for(FTPFile file: files){
if(file.getName().equals(fileToBeUpload.getName())){
//copy file to backupDirectory
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑过的代码:仍有问题,当我备份zip文件时,备份文件已损坏.
有没有人知道它的原因?
public static void backupUploadWithCommonsFTP(File fileToBeUpload) {
FTPClient f = new FTPClient();
boolean backupDirectoryExist = false;
boolean fileToBeUploadExist = …Run Code Online (Sandbox Code Playgroud) 我必须将文件上传到FTP服务器.文件名包含特殊字母äöü.在FTP服务器上,我需要文件名为UTF-8编码.
我的代码是这样的:
import org.apache.commons.net.ftp.FTPClient;
FTPClient client = new FTPClient();
...
boolean retval = client.storeFile(fileName, inputStream);
Run Code Online (Sandbox Code Playgroud)
问题是之后storeFile,FTP服务器上保存的文件名是ISO-8859-1编码而不是UTF-8.
如何告诉FTPClientUTF-8编码文件名?
以下是创建文本文档并将其上传到我的FTP服务器的代码.由于某种原因,它似乎不起作用.我习惯了提供的图书馆
http://lavalatwork.blogspot.tw/2010/09/using-apache-commons-ftp-library-in.html
用于与FTP服务器通信.
try
{
final String testString = new String("Hello");
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(testString);
osw.flush();
osw.close();
}
catch(IOException ex)
{
}
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("192.168.10.101");
//mFTP.login("user", "password");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
// Prepare file to be uploaded to FTP Server
File file = new File(getFileStreamPath("samplefile.txt")+ "");
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
mFTP.storeFile("filetotranfer",ifile);
mFTP.disconnect();
} catch (SocketException e) {
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用XAMPP和FileZilla通过FTP上传一个简单的txt文件.
我正在使用Apache Commons Net 3.0.1库.
这是我的代码,非常基本的东西:
FTPClient client = new FTPClient();
InputStream in = new ByteArrayInputStream("IT WORKS! :D".getBytes());
try {
client.connect("localhost");
client.login("user", "password");
client.enterLocalPassiveMode();
client.storeFile("textfile.txt", in);
} finally {
try {
in.close();
client.logout();
client.disconnect();
} catch (Exception e) {
}
}
Run Code Online (Sandbox Code Playgroud)
但是... storeFile()抛出一个java.net.SocketException:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.read(BufferedReader.java:175)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:310)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:474)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:596) …Run Code Online (Sandbox Code Playgroud) java ×10
ftp ×9
ftp-client ×4
android ×2
binary-data ×1
filezilla ×1
ftps ×1
utf-8 ×1
xampp ×1