代码如下: String url = "10.100.xxx.xx";
int port = 21;
String ftpusername = "xxxxxxxxxxx";
String ftppassword = "xxxxxxxxxxx";
String path = "xxxxxxxxxxxxxxxx";
FTPClient ftp = new FTPClient();
try {
int reply;
FileInputStream fileUp=new FileInputStream(new File("a.xls"));
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(ftpusername, ftppassword);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
ftp.changeWorkingDirectory(path);
ftp.storeFile("a.xls", fileUp);
fileUp.close();
ftp.logout();
System.out.println("ok");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
} 本地可以正常打开a.xls,ftp服务器上也能看到a.xls的文件,但是打开就提示文件损坏。如果上传txt文件,就可以正常打开。什么问题呢?
int port = 21;
String ftpusername = "xxxxxxxxxxx";
String ftppassword = "xxxxxxxxxxx";
String path = "xxxxxxxxxxxxxxxx";
FTPClient ftp = new FTPClient();
try {
int reply;
FileInputStream fileUp=new FileInputStream(new File("a.xls"));
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(ftpusername, ftppassword);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
ftp.changeWorkingDirectory(path);
ftp.storeFile("a.xls", fileUp);
fileUp.close();
ftp.logout();
System.out.println("ok");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
} 本地可以正常打开a.xls,ftp服务器上也能看到a.xls的文件,但是打开就提示文件损坏。如果上传txt文件,就可以正常打开。什么问题呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货