这是我的代码
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public void connectServer(String ip, int port, String username,String password, String path){
try{/*连接服务器的两种方法*/
ftpClient = FtpClient.create();
try{
SocketAddress addr = new InetSocketAddress(ip,port);
ftpClient.connect(addr);
ftpClient.login(username, password.toCharArray());
System.out.println("登陆成功!");
if(path.length() != 0){
//吧远程系统上的目录切换到参数path所指定的目录
ftpClient.changeDirectory(path);// throws FtpProtocolException, IOException
}
}catch(FtpProtocolException e){
//TODO Auto-generated catch block
e.printStackTrace();
System.out.println(0000000);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void download(String remoteFile,String localFile){
InputStream is = null;
FileOutputStream fos = null;
System.out.println(111111111);
try{
ftpClient = FtpClient.create();
//获取远程机器上的文件filename,借助TelnetInputStream
try{
is = ftpClient.getFileStream(remoteFile);
}catch(FtpProtocolException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
//File file_in = new File(localFile);
fos = new FileOutputStream(localFile);//localFile 为内存输出流的输出对象
byte[] bytes = new byte[204800];
int c;
while((c = is.read(bytes)) != -1){
fos.write(bytes, 0, c);
}
System.out.println("download success!");
}catch(IOException ex){
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
}finally{
try{
if(is != null){
is.close();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fos != null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
文件能下载下来但是不能打开,如图,不管点是还是否,都不能出来数据
帖子有点早,近期也遇到同样的问题,解决方式:ftp.login() 后 设置数据传输类型为二进制 ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
默认ascii,而对于ascii形式的数据,对于行结束符和回车之类的编码win(\r\n)和linux(\n)有区别
说明服务器写出去出错了。
无法打开提示什么错误?你是用什么软件打开的
http://www.360doc.com/content/11/0804/11/1106320_137859916.shtml
http://miaoxianjie.iteye.com/blog/1158626
下载是成功了,可是下载的文件数据有问题。
你可以用二进制比较工具(比如WinDiff或者UltraCpmparer)看下是文件不完整还是文件内容有问题。
byte[] bytes = new byte[204800];//你把这里的大小改小点试试吧。。。FTP协议本身并不是很优美的协议,自身就是有缺陷的。。。你可以下个md5工具,对源文件和下载后的文件做比对,看文件是否是在下载过程中出的问题。如果比对一致,基本可以判断为服务端原文件就有问题。。。可以用ftp工具去连接ftp服务下载文件试试,看能打开不。。。。
从代码上,你的代码没有什么问题。