java通过ftp进行文件的上传下载(二)

2014-11-24 03:16:45 · 作者: · 浏览: 2
, i - j);

namesList.add(tempName);

// System.out.println(temp);

// 处理代码处

// j = i + 2; //上一次位置二进制模式

j = i + 1; // 上一次位置字符模式

}

i = i + 1;

}

return namesList;

}

/**

* 上传文件到FTP服务器,destination路径以FTP服务器的"/"开始,带文件名、 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖

*

* @param source

* String

* @param destination

* String

* @throws Exception

*/

public void upFile(String source, String destination) throws Exception {

buildList(destination.substring(0, destination.lastIndexOf("/")));

ftpclient.binary(); // 此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclient.put(destination);

TelnetInputStream ftpIn = new TelnetInputStream(new FileInputStream(

source), true);

byte[] buf = new byte[204800];

int bufsize = 0;

while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {

ftpOut.write(buf, 0, bufsize);

}

ftpIn.close();

ftpOut.close();

}

/**

* JSP中的流上传到FTP服务器, 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖 字节数组做为文件的输入流,此方法适用于JSP中通过

* request输入流来直接上传文件在RequestUpload类中调用了此方法,destination路径以FTP服务器的"/"开始,带文件名

*

* @param sourceData

* byte[]

* @param destination

* String

* @throws Exception

*/

public void upFile(byte[] sourceData, String destination) throws Exception {

buildList(destination.substring(0, destination.lastIndexOf("/")));

ftpclient.binary(); // 此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclient.put(destination);

ftpOut.write(sourceData, 0, sourceData.length);

// ftpOut.flush();

ftpOut.close();

}

/**

* 从FTP文件服务器上下载文件SourceFileName,到本地destinationFileName 所有的文件名中都要求包括完整的路径名在内

*

* @param SourceFileName

* String

* @param destinationFileName

* String

* @throws Exception

*/

public void downFile(String SourceFileName, String destinationFileName)

throws Exception {

ftpclient.binary(); // 一定要使用二进制模式

TelnetInputStream ftpIn = ftpclient.get(SourceFileName);

byte[] buf = new byte[204800];

int bufsize = 0;

FileOutputStream ftpOut = new FileOutputStream(destinationFileName);

while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {

ftpOut.write(buf, 0, bufsize);

}

ftpOut.close();

ftpIn.close();

}

/**

* 从FTP文件服务器上下载文件,输出到字节数组中

*

* @param SourceFileName

* String

* @return byte[]

* @throws Exception

*/

public byte[] downFile(String SourceFileName) throws Exception {

ftpclient.binary(); // 一定要使用二进制模式

TelnetInputStream ftpIn = ftpclient.get(SourceFileName);

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

byte[] buf = new byte[204800];

int bufsize = 0;

while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {

byteOut.write(buf, 0, bufsize);

}

byte[] return_arraybyte = byteOut.toByteArray();

byteOut.close();

ftpIn.close();

return return_arraybyte;

}

/**

* 调用示例FtpUpfile fUp = new FtpUpfile("192.150.189.22", 21, "admin",

* "admin"); fUp.login(); fUp.buildList("/adfadsg/sfsdfd/cc"); String

* destination = "/test.zip"; fUp.upFile("C:\\Documents and

* Settings\\Administrator\\My Documents\\sample.zip",destination);

* ArrayList filename = fUp.fileNames("/"); for (int i = 0; i <

* filename.size(); i++) { System.out.println(filename.get(i).toString()); }

* fUp.logout();

*

* @param args

* String[]