如何从FTP删除文件

时间:2012-06-21 05:10:35

标签: java ftp

任何人都可以建议如何从FTP服务器删除文件。我不想使用Apache FTP Client。

对于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;
        }

public file download(String ftpServer,String user,String password,             String fileName)抛出MalformedURLException,             IOException异常             {         if(ftpServer!= null&& fileName!= null)         {             文件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;
        }

0 个答案:

没有答案