任何人都可以建议如何从 FTP 服务器中删除文件。我不想使用 Apache FTP 客户端。
对于 FTP 连接和从 FTP 下载文件,我使用以下代码:
URL url = new URL("ftp://"+userName+":"+password+"@"+url12+"/"+folderLocation+";type=i");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
try {
while ((line = reader.readLine()) != null) {
if ( line.endsWith(".dat")) {
String fileName = getFileNameFromFTP(line, file_Name);
if ( ! TCUtility.isStringNullOrEmpty(fileName)){
File file = download(url12,userName,password, folderLocation+"/"+fileName);
if ( file != null) {
fileList.add(file);
}
}
}
}
urlc.setRequestProperty("Connection", "Close");
} catch (IOException e) {
e.printStackTrace();
ex = e;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
ex = e;
}
if ( ex != null )
throw ex;
}
公共文件下载(字符串 ftpServer,字符串用户,字符串密码,字符串文件名)抛出 MalformedURLException,IOException { if (ftpServer != null && fileName != null) { File file = File.createTempFile("TempDoc", ".dat") ;
StringBuffer sb = new StringBuffer( "ftp://" );
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append( user );
sb.append( ':' );
sb.append( password );
sb.append( '@' );
}
sb.append( ftpServer );
sb.append( '/' );
sb.append( fileName );
/*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing
*/
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new FileOutputStream(file ) );
int i;
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
return file;
}
return null;
}