import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test;
public class FTPTest {
@Test
public void testFtpClient() throws Exception{
//創建一個FTPClient對象
FTPClient ftpClient = new FTPClient();
//創建ftp連接。默認是21端口
ftpClient.connect("192.168.44.128", 21);//hostname:地址,21是端口號
//登錄ftp服務器,使用用戶名和密碼。
ftpClient.login("ftpuser", "ftpuser");
//上傳文件。
FileInputStream inputStream = new FileInputStream(new File("D:\\1.txt"));
//設置上傳的路徑
ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
//修改上傳文件的格式(默認是文本格式,圖片是二進制格式,所以要改二進制格式)
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//第一個參數:服務器端文件名
//第二個參數:上傳文件的inputStream(IO流)
ftpClient.storeFile("hello1.txt", inputStream);
//關閉連接。
ftpClient.logout();
}
}
java.io.FileNotFoundException: D:\1.txt (文件名、目錄名或卷標語法不正確。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at com.taotao.controller.FTPTest.testFtpClient(FTPTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
盤符D寫成小寫的d試試看