编程语言
首页 > 编程语言> > java代码实现FTP方式上传文件

java代码实现FTP方式上传文件

作者:互联网

1、需要的依赖jar包


        <!-- https://mvnrepository.com/artifact/commons-net/commons-net-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

2、实现代码

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
 
/**
 * Created by Administrator on 2017\10\30 0030.
 */
public class test {
 
    public static void main(String[] args){
        Upload("xx.xxx.xxx.xx",21,"xxxx","xxxxx","D:\\123.xls","1123.xls");
    }
 
    /**
     * 需要的参数 依次是  ftp地址  ftp端口  ftp用户名  ftp密码 想要上传的文件路径  上传后文件的名称
     * @param url
     * @param port
     * @param username
     * @param password
     * @param filePath
     * @param ftpFile
     */
    public static void Upload(String url,Integer port,String username,String password,String filePath,String ftpFile) {
        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;
        try {
            ftpClient.connect(url,port);
            ftpClient.login(username, password);
 
            File srcFile = new File(filePath);
            fis = new FileInputStream(srcFile);
            
            String fileTimeName = String.valueOf(new Date().getTime());
            //设置上传目录
             //在根目录下创建003文件夹
            ftpClient.makeDirectory("003");
             //切换工作空间到003文件夹下
            ftpClient.changeWorkingDirectory("003/");
             //创建 文件名为 fileTimeName 的文件夹
            ftpClient.makeDirectory(fileTimeName);
             //切换工空间带该目录下
            ftpClient.changeWorkingDirectory(fileTimeName+"/");
              //创建 文件名为 FF-FF-FF-FF-FF-FF 的文件夹
            ftpClient.makeDirectory("FF-FF-FF-FF-FF-FF");
            //切换工空间带该目录下
            ftpClient.changeWorkingDirectory("FF-FF-FF-FF-FF-FF/");
            
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
 
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            boolean flag = ftpClient.storeFile(new String(ftpFile.getBytes("UTF-8"),"iso-8859-1"),fis);
            //如果文件上传成功 则再上传一个 标记文件 [也可以不要]
            FileInputStream fis_2 = null;
            if(flag){
                fis_2=new FileInputStream(new File(filePath));
                boolean flag_1 =ftpClient.storeFile(new String((ftpFile+".ok").getBytes("UTF-8"),"iso-8859-1"),fis_2);
            }
            fis.close();
            fis_2.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
    }
}

标签:FTP,java,String,fis,param,FF,new,上传,ftpClient
来源: https://blog.csdn.net/weixin_45958605/article/details/119103098