栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java使用ftp上传文件示例分享

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用ftp上传文件示例分享

复制代码 代码如下:
import java.io.ByteArrayInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.SocketException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import org.apache.commons.io.IOUtils; 
import org.apache.commons.net.ftp.FTPClient; 

 
public class FTPFileTransmit { 

    private String ftpPath; 
    private String ftpName; 
    private String ftpPassword; 
    private String ftpServerIP; 

    public FTPFileTransmit() { 
        this.ftpPath = "xxx/xxx/"; 
        this.ftpName = "name"; 
        this.ftpPassword = "pass"; 
        this.ftpServerIP = "192.168.0.xx"; 
    } 

     
     
    public boolean saveInFTP (String FolderName, String FileName, byte[] data) { 
        boolean flag = false; 

        // 创建FTP客户端  
        FTPClient ftpClient = new FTPClient(); 
        // 输入流用于读取文件  
//      FileInputStream fis = null;  
        ByteArrayInputStream bis = null; 

        try { 
            // 如果FolderName 和 FileName都不符合基本要求, 那么就没有必要进行ftp操作  
            if (FolderName != null 
                    && FolderName.compareTo("") != 0 
                    && FileName != null 
                    && FileName.compareTo("") != 0) { 

                // 建立FTP连接  
                ftpClient.connect(this.ftpServerIP); 

                // 如果登录成功后, 才进行创建输入流  
                if (ftpClient.login(this.ftpName, this.ftpPassword)) { 
//                  File srcClientFile = new File("C:/ParseXML.xml");  

                    // 实例化输入流  
//                  fis = new FileInputStream(srcClientFile);  

                    if (ftpClient.changeWorkingDirectory(FolderName)) { 
                        // 将byte[]写入到输入流中, 实例化  
                        bis = new ByteArrayInputStream(data); 

                        // 设置缓冲  
                        ftpClient.setBufferSize(1024); 

                        // 设置文件类型(二进制类型)  
                        if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) { 
                            flag = ftpClient.storeFile(FileName, bis); 
                        } 
                    } 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            flag = false; 
        } finally { 
            try { 
                // 关闭输入流  
                IOUtils.closeQuietly(bis); 
                // 关闭连接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return flag; 
    } 

     
    public boolean getFromFTP () { 
        boolean flag = false; 

        // 创建FTP客户端  
        FTPClient ftpClient = new FTPClient(); 
        // 输出流用于输出文件  
        FileOutputStream fos = null; 

        try { 
            // 建立FTP连接  
            ftpClient.connect(this.ftpServerIP); 
            // 如果登录成功后, 才进行创建输出流  
            if (ftpClient.login(this.ftpName, this.ftpPassword)) { 
                // FTP文件  
                String distinationFile = "/name/xxx/xxx/xxx文件"; 
                // 实例化输出流  
                fos = new FileOutputStream("C:/ParseXML_InFTP.xml"); 

                // 设置缓冲  
                ftpClient.setBufferSize(1024); 

                // 设置文件类型(二进制类型)  
                if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) { 
                    ftpClient.retrieveFile(distinationFile, fos); 
                    flag = true; 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            flag = false; 
        } finally { 
            try { 
                // 关闭输出流  
                IOUtils.closeQuietly(fos); 
                // 关闭连接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return flag; 
    } 

    public boolean createDirectory() { 
        boolean flag = false; 

        // 创建FTP客户端  
        FTPClient ftpClient = new FTPClient(); 

        try { 
            // 建立FTP连接  
            ftpClient.connect(this.ftpServerIP); 
            // 如果登录成功后, 才进行操作  
            if (ftpClient.login(this.ftpName, this.ftpPassword)) { 

                // 切换文件路径, 到FTP上的"NNDD3"文件夹下  
                if (this.ftpPath != null && this.ftpPath.compareTo("") != 0 
                        && ftpClient.changeWorkingDirectory(this.ftpPath)) { 
                    SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd"); 
                    String time = f.format(new Date()); 

                    String FolderName = time + "_ReTransmit"; 
                    ftpClient.makeDirectory(FolderName); 
                    flag = true; 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            flag = false; 
        } finally { 
            try { 
                // 关闭连接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return flag; 
    } 

    public String[] getAllFolderNames () { 
        // 创建FTP客户端  
        FTPClient ftpClient = new FTPClient(); 

        try { 
            // 建立FTP连接  
            ftpClient.connect(this.ftpServerIP); 

            // 如果登录成功后, 才进行操作  
            if (ftpClient.login(this.ftpName, this.ftpPassword)) { 

                // 切换文件路径, 到FTP上的"NNDD3"文件夹下  
                if (this.ftpPath != null && this.ftpPath.compareTo("") != 0 
                        && ftpClient.changeWorkingDirectory(this.ftpPath)) { 
                    // 将当前时间减去2天, 删除的是前两天的数据包  
                    String time = minusTime(); 

                    String[] allNames = ftpClient.listNames(); 

                    String[] temp = new String[allNames.length]; 
                    // 初始化数组  
                    for (int j = 0; j < allNames.length; j ++) { 
                        temp[j] = ""; 
                    } 

                    // 找出要删除文件夹的名称  
                    for (int i = 0; i < allNames.length; i ++) { 
                        if (allNames[i].substring(0, 8).compareTo(time) <= 0) { 
                            temp[i] = allNames[i]; 
                        } 
                    } 

                    return temp; 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                // 关闭连接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return null; 
    } 

     
    private String minusTime() { 
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");    
        Date d = new Date(); 
        String timeMinus2 = df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000));    
        return timeMinus2; 
    } 

    public static void main(String[] args) { 
        FTPFileTransmit ftpFileTransmit = new FTPFileTransmit(); 
        ftpFileTransmit.deleteFoldersInFTP(); 

//      boolean flag = ftpFileTransmit.createDirectory();  
//      if (flag) {  
//          System.out.println("****** FTP文件夹创建成功 ******");  
//      }  

//      String FolderName = ftpFileTransmit.ftpPath + "20110809_ReTransmit/";  
//      byte[] data = new byte[1024];  
//      for (int i = 0; i < data.length; i ++) {  
//          data[i] = 'a';  
//      }  
//      boolean flag = ftpFileTransmit.saveInFTP(FolderName, "2011080912345678" ,data);  
//      if (flag) {  
//          System.out.println("****** FTP文件夹创建成功 ******");  
//      }  
    } 
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号