日期:2014-05-17 浏览次数:20889 次
/**
* 上传文件或文件夹
*/
public void upload(File file, ProgressListener listener) throws Exception
{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
/**
* File.isDirectory():
* Tests whether the file denoted by this abstract pathname is a directory
* 测试当前file对象表示的文件是否是一个路径
*/
if(file.isDirectory())
{
/**
* File.getName()
* Returns the name of the file or directory denoted by this abstract pathname
* 返回表示当前对象的文件名
*/
ftpClient.makeDirectory(file.getName());
ftpClient.changeWorkingDirectory(file.getName());
/**
* File.list()
* Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname
* 返回当前File对象指定的路径文件列表
*/
String[] files = file.list();
for(int i=0; i<files.length; i++)
{
/**
* File.getPath()
* Converts this abstract pathname into a pathname string.
* 输出:D:\a\x.flv
*/
File file1 = new File(file.getPath()+"\\"+files[i]);
if(file1.isDirectory())
{
upload(file1, listener);
ftpClient.changeToParentDirectory();
}
else
{
File file2 = new File(file.getPath()+"\\"+files[i]);
System.out.println("else分支中 " + file.getPath()+"\\"+files[i]);
System.out.println("else分支中 file2.getName() = " + file2.getName());
InputStream input = new FileInputStream(file2);
int n = -1;
/**
* InputStream.available()
* Returns an estimate of the number of bytes that can be read(or slipped over) from this input stream
* without blocking by the next invocation of a method for this input stream
*/
long pContentLength = input.available();
System.out.println("else分支中 pContentLength= " + pContentLength);
System.out.println("--------------------------------------------");
long trans = 0;
/**
* bufferSize = 1024
*/
int bufferSize = ftpClient.getBufferSize();
byte[] buffer = new byte[bufferSize];
OutputStream os = ftpClient.storeFileStream(file2.getName());
/**
* FileInputStream.read(byte[] b)
* Reads&nb