File file = new File("c:\\test.properties");//这一步是按需要来指定的,可以把整个目录中文件上传,也可以指定某个文件,也可以通过文件选择对话框。ftp.storeFile("test.properties",new FileInputStream(file)); 参数说明: test.properties --在服务端创建的文件名 new FileInputStream(file) --上传文件流数据
这个import sun.net.TelnetInputStream是要自己找吗?
我测试了,出现了文件找不到的异常,就是这句File file = new File(src);我是通过文件选择框选择之后传递过来的,文件明明是存在的,为什么说找不到阿?
出错代码如下: java.io.FileNotFoundException: E:\Page.htm (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at _ftpexe__jsp._jspService(/sanxia/ftpexe.jsp:24) at com.caucho.jsp.JavaPage.service(JavaPage.java:75) at com.caucho.jsp.Page.subservice(Page.java:506) at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182) at com.caucho.server.http.Invocation.service(Invocation.java:315) at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135) at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:344) at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274) at com.caucho.server.TcpConnection.run(TcpConnection.java:139) at java.lang.Thread.run(Thread.java:534)
import java.net.InetAddress;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.ftp.parser.*;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileEntryParser;import sun.net.TelnetInputStream;public class FtpAppache {
public FtpAppache() throws Exception{
// sun.net.ftp.FtpClient ft = null;
// TelnetInputStream t = ft.list();
// t.setStickyCRLF(true);
} public void test1() throws Exception {
//
String strTemp = ""; //InetAddress ia = InetAddress.getByName("192.168.0.193");
FTPClient ftp = new FTPClient();
ftp.connect("192.168.0.193"); boolean blogin = ftp.login("de", "123456");
if (!blogin) {
System.out.println("连接失败");
ftp.disconnect();
ftp = null;
return;
} /*//如果是中文名必需进行字符集转换
boolean bMakeFlag = ftp.makeDirectory(new String("测试目录".getBytes(
"gb2312"), "iso-8859-1")); //在服务器创建目录
//上传文件到服务器,目录自由创建
File file = new File("c:\\test.properties");
ftp.storeFile("test.properties",new FileInputStream(file));*/ System.out.println(ftp.getSystemName()); FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null) {
for (int i = 0; i < ftpFiles.length; i++) {
System.out.println(ftpFiles[i].getName());
//System.out.println(ftpFiles[i].isFile());
if (ftpFiles[i].isFile()) {
FTPFile ftpf = new FTPFile();
/*System.err.println(ftpf.hasPermission(FTPFile.GROUP_ACCESS,
FTPFile.EXECUTE_PERMISSION));
System.err.println("READ_PERMISSION="+ftpf.hasPermission(FTPFile.USER_ACCESS,
FTPFile.READ_PERMISSION));
System.err.println("EXECUTE_PERMISSION="+ftpf.hasPermission(FTPFile.USER_ACCESS,
FTPFile.EXECUTE_PERMISSION));
System.err.println("WRITE_PERMISSION="+ftpf.hasPermission(FTPFile.USER_ACCESS,
FTPFile.WRITE_PERMISSION));
System.err.println(ftpf.hasPermission(FTPFile.WORLD_ACCESS,
FTPFile.READ_PERMISSION));*/ }
//System.out.println(ftpFiles[i].getUser());
}
} //下载服务器文件
FileOutputStream fos = new FileOutputStream("e:/proftpd-1.2.10.tar.gz");
ftp.retrieveFile("proftpd-1.2.10.tar.gz",fos);
fos.close(); //改变ftp目录
//ftp.changeToParentDirectory();//回到父目录
//ftp.changeWorkingDirectory("");//转移工作目录
//ftp.completePendingCommand();// //删除ftp服务器文件
//ftp.deleteFile(""); //注销当前用户,
//ftp.logout();
//ftp.structureMount("");
ftp.disconnect();
ftp = null;
} /**
* 封装好的东西是好用,碰到这种问题,我们就up
* @param args String[]
*/
public static void main(String[] args) {
try {
FtpAppache ftpAppache1 = new FtpAppache();
ftpAppache1.test1();
}
catch (Exception e) {
e.printStackTrace();
}
}}
参数说明:
test.properties --在服务端创建的文件名
new FileInputStream(file) --上传文件流数据
java.io.FileNotFoundException: E:\Page.htm (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at _ftpexe__jsp._jspService(/sanxia/ftpexe.jsp:24)
at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
at com.caucho.jsp.Page.subservice(Page.java:506)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:344)
at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:534)