下载部分代码: public synchronized boolean download(String remoteFile,String localFile) throws IOException {
dataSocket = createDataSocket();
sendCommand("RETR " + remoteFile);
response = readLine();
if (!response.startsWith("1")) {
System.out.println(response);
}
System.out.println(localFile);
FileOutputStream fileOut = new FileOutputStream(localFile);
BufferedInputStream dataIn = new BufferedInputStream(dataSocket.getInputStream());
new File(localFile).createNewFile();
byte[] buffer = new byte[4096];
int bytesRead = 0;
do {
bytesRead = dataIn.read(buffer);
if (bytesRead != -1) {
fileOut.write(buffer, 0, bytesRead); //将byte[]数组下标0开始的bytesRead
//长度的数据写入当前输出流
}
} while (bytesRead != -1);
fileOut.flush();
fileOut.close();
dataSocket.close();
response = readLine();
if (response.startsWith("226")) {
JOptionPane.showConfirmDialog(null,
"下载成功",
" 下载信息", JOptionPane.CLOSED_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
return (response.startsWith("226"));
}
``` public boolean uploadFile(String path,FileInputStream in) throws Exception { DataConnect dc=getDataStream(2);
try {
String line=sendCmd("STOR "+path+"\r\n");
if(line.startsWith("125")) {
OutputStream out=dc.getDataOut();
int len=0;
byte[]bb=new byte[1024*4];
while((len=in.read(bb))>0) {
out.write(bb,0,len);
}
return true;
}else {
return false;
}
}catch(Exception e) {
e.printStackTrace();
}finally {
dc.destoryOutput();
}
return false;
}