FileInfo(FileInfo.DOS,parts[0],parts[1],parts[2],parts[3],isDir);
}else{
System.out.println(\ return null; }
}
//文件目录的主要操作功能代码
private void jbInit() throws Exception {
popMenu = new JPopupMenu();
jLabel5.setText(\服务器文件/目录列表:\ jLabel6.setText(\状态信息:\
popMenu.add(item = new JMenuItem(\刷新目录\ item.addActionListener(popMenuListener);
popMenu.add(item =new JMenuItem(\上载文件\ item.addActionListener(popMenuListener);
popMenu.add(item =new JMenuItem(\下载文件\ item.addActionListener(popMenuListener);
popMenu.add(item =new JMenuItem(\新建文件夹\ item.addActionListener(popMenuListener); popMenu.add(item =new JMenuItem(\删除\ item.addActionListener(popMenuListener); popMenu.add(item =new JMenuItem(\重命名\ item.addActionListener(popMenuListener);
图 6 目录图
4.4 文件的上传与下载
通过浏览程序对FTP服务器和本地文件系统的浏览,解决了客户端和服务器端的文件和目录选择的问题,但是文件数据是需要传送和交换的,下面我们将介绍如何实现文件的下载和上传等操作过程。
4.4.1文件下载程序
当我们想下载文件,可以使用TelnetInputStream流,并使用FTPClient控件的get方法将流的源头绑定,再将TelnetInputStream流绑定到DataInputStream流中,然后再在本地新建一个文件,并绑定到RandomAccessFile流中,执行DataInputStream流的读并写入RandomAccessFile流中,就可以完成下载功能了。
当然,程序首先需要判断本地浏览框中选中的是目录还是文件,或者根本就没有在本地浏览框中选择。为了可同时选择多个文件下载,必须先设置list1控件的MultipleMode属性为true,以便能够在远程浏览框中进行多重选择。
接着在程序中利用list1控件对象的getSelectedItems方法获取所选择的文件和目录,并判断是否为目录;若是目录,则在本地创建一个目录(本实例没有使用递归的方法,所以不能下载目录中的文件和子目录),否则,将进行文件下载。一直重复至所有被选择文件或者目录都被完成操作为止。在这里,使用了一个isfile的boolean变量用于判断是否为文件。
4.4.2文件上传程序
上传文件基本上与下载文件类似,不过使用的是OutputStream流,并使用FTPClient控件的put方法和流绑定,再将OutputStream流绑定到DataOutputStream流中,然后在本地新建一个文件,并绑定到RandomAccessFile流中,执行RandomAccessFile流的读并写入DataOutputStream流中,就可以完成上传功能了。代码如下:
public int dataSocketIn(String datCmd, RandomAccessFile f) throws IOException{
int status;
status = dataSocket(datCmd); if(status!=0) return status;
//datReader = new InputStreamReader(datskt.getInputStream()); InputStream inStream = datskt.getInputStream();
// 传输数据直到数据关闭 byte[] byteBuffer = new byte[1024]; int amount; int debugCount = 0; do{
amount = inStream.read(byteBuffer); debugCount+=amount;
//System.out.println(\ if(amount!=-1) f.write(byteBuffer,0,amount); }while(amount!=-1);
System.out.println(\ inStream.close(); datskt.close(); return getResponse();
}
case \下载文件\ {
//download files
if(remoteList.isSelectionEmpty()) return; if(fc == null)
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
int option = fc.showSaveDialog(bDownload); String saveFilePath;
if(option == JFileChooser.APPROVE_OPTION){ //user pressed SAVE button
saveFilePath = fc.getSelectedFile().toString(); }else{
//cancelled return;
} Object[] fileList = (Object []) remoteList.getSelectedValues();
for (Object fileList1 : fileList) {
String fileInfo = fileList1.toString(); String name =
ftpDrive.getFileName(fileInfo,FileInfo.nameStartAt);
if(fileInfo.charAt(0)=='d')
ftpDrive.downloadFold(name, saveFilePath); if(fileInfo.charAt(0)=='-')
ftpDrive.downloadFile(name,saveFilePath+\
if(fileInfo.charAt(0)=='g') { } }
statusList.append(\ break; }
case \上载文件\ {
//upload files if(fc==null)
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
String files;
int option=fc.showSaveDialog(bDownload);
if(option == JFileChooser.APPROVE_OPTION){ //user pressed SAVE button
files = fc.getSelectedFile().toString(); }else{
//cancelled return;
} File targetFile = new File(files); ftpDrive.uploadFile(targetFile); updateList();
statusList.append(\ break; }
实现从另一台电脑上下载文件到自己的电脑上:
图7 文件上传前
上传成功后,文件在服务器目录列表中成功显示:
图8 文件上传后
然后是下载文件功能的测试,进入到music文件夹下,选择张学友-慢慢mp3 文件,点击下载选择保存至桌面,如图9: