在单位写一个上传文件到一家合作公司ftp服务器的代码,奇怪的是,我的代码上传自己单位的window ftp 或者unix服务器都没有问题,但是上传给合作公司的服务器就失败,真搞不懂.......
这是我的代码,麻烦哪位高人指点一下/*
* FTP上传
* 参数说明:
* ip:上传目标文件的ip地址
* port:端口号
* username:用户名
* pwd:密码
* targetfile:要上传的文件
* upfile: 指定上传后文件保存在服务器上的名字 (完整路径)
*/
public void myftp(String ip,int port,String username,String pwd,String targetfile,String upfile)
{
FTPClient ftpClient = new FTPClient();
try
{
ftpClient.connect(ip, port);
if(ftpClient.isConnected())
{
System.out.println("FTP服务器连接成功");
System.out.println("FTP服务器信息为:" + ftpClient.getReplyString());
}
else
{
System.out.println("FTP服务器连接失败");
}
boolean loginflag=ftpClient.login(username, pwd);
if(loginflag)
{
System.out.println("登录FTP服务器成功");
System.out.println("要上传的文件为:"+targetfile);
System.out.println("上传写入的完整路径为:"+upfile);
ftpClient.enterLocalPassiveMode(); //PASV(被动)方式的连接
//ftpClient.enterLocalPassiveMode();
//ftpClient.enterRemotePassiveMode();
//ftpClient.enterLocalActiveMode();
//ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //设置bin方式传送
}
else
{
System.out.println("登录FTP服务器失败!! 请检查......");
}
//指定用户使用的工作目录
//ftpClient.changeWorkingDirectory("E://myspace/ rv-U//");
//指定要上传的文件
FileInputStream file = new FileInputStream(targetfile);
//指定上传后文件保存在服务器上的名字
boolean fileupflag=ftpClient.storeUniqueFile(upfile, file);
System.out.println("上传是否成功:"+fileupflag); file.close();
ftpClient.logout();
ftpClient.disconnect();
System.out.println("和FTP服务器断开连接");
}
catch(SocketException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
System.err.println(e.toString());
}
}
这是我的代码,麻烦哪位高人指点一下/*
* FTP上传
* 参数说明:
* ip:上传目标文件的ip地址
* port:端口号
* username:用户名
* pwd:密码
* targetfile:要上传的文件
* upfile: 指定上传后文件保存在服务器上的名字 (完整路径)
*/
public void myftp(String ip,int port,String username,String pwd,String targetfile,String upfile)
{
FTPClient ftpClient = new FTPClient();
try
{
ftpClient.connect(ip, port);
if(ftpClient.isConnected())
{
System.out.println("FTP服务器连接成功");
System.out.println("FTP服务器信息为:" + ftpClient.getReplyString());
}
else
{
System.out.println("FTP服务器连接失败");
}
boolean loginflag=ftpClient.login(username, pwd);
if(loginflag)
{
System.out.println("登录FTP服务器成功");
System.out.println("要上传的文件为:"+targetfile);
System.out.println("上传写入的完整路径为:"+upfile);
ftpClient.enterLocalPassiveMode(); //PASV(被动)方式的连接
//ftpClient.enterLocalPassiveMode();
//ftpClient.enterRemotePassiveMode();
//ftpClient.enterLocalActiveMode();
//ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //设置bin方式传送
}
else
{
System.out.println("登录FTP服务器失败!! 请检查......");
}
//指定用户使用的工作目录
//ftpClient.changeWorkingDirectory("E://myspace/ rv-U//");
//指定要上传的文件
FileInputStream file = new FileInputStream(targetfile);
//指定上传后文件保存在服务器上的名字
boolean fileupflag=ftpClient.storeUniqueFile(upfile, file);
System.out.println("上传是否成功:"+fileupflag); file.close();
ftpClient.logout();
ftpClient.disconnect();
System.out.println("和FTP服务器断开连接");
}
catch(SocketException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
System.err.println(e.toString());
}
}
* Description: 向FTP服务器上传文件
* @Version1.0
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 返回上传状态 StatusCodes 成功与否
*/
public String uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
String error_age=StatusCodes.DownloadCompleted;
FTPClient ftp = new FTPClient();
try {
int reply;
logger.info(url+" "+port);
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
ftp.setFileType(BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
ftp.setFileTransferMode( FTP.STREAM_TRANSFER_MODE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
error_age=StatusCodes.ErrorOnUpload;
return error_age;
}
if(!ftp.changeWorkingDirectory(path))
{
File newFile = new File(path +"/"+ filename);
String dir = newFile.getParentFile().getPath();
if (!ftp.changeWorkingDirectory(dir)) {
if (!ftp.makeDirectory(newFile.getParentFile().getPath())) {
logger.debug("创建文件目录【" + dir + "】 失败!");
}
}
}
logger.info(path+"/"+filename);
ftp.storeFile(path+"/"+filename, input);
input.close();
ftp.logout();
error_age=StatusCodes.UploadCompleted;
} catch (IOException e) {
error_age=StatusCodes.ErrorOnUpload;
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
error_age=StatusCodes.ErrorOnUpload;
}
}
} return error_age; }