Home > Java > javaTutorial > How to upload files to FTP in java

How to upload files to FTP in java

PHPz
Release: 2023-05-13 09:52:05
forward
2536 people have browsed it

需求说明:将指定文件上传到FTP,上传成功后修改文件名。

获取文件名及路径(这里是从数据库获取,所以前面的代码就不CV了)

/**
     * 测试上传
     * @param map 从数据库获取到的文件信息 (包含文件路径FILE_PATH 文件类型FILE_TYPE等信息)
     */
     public void testUpdFtpFile(Map<String,Object> map){
         /*上传附件到FTP*/
         FileInputStream inputStream = null;
         try {
            //找到要上传的文件 
             String originfilename = "E:\\work\\files\\"+map.get("FILE_PATH").toString();
             //转成流
             inputStream = new FileInputStream(originfilename);
            //上传后的文件名+文件类型                        
            String ftpName = "上传到FTP后的文件名."+map.get("FILE_TYPE");
             boolean updFtpFile = FtpClientFile.uploadFile(ftpName,inputStream);
             if(updFtpFile){
                 //打印下日志
                 System.out.println(("=======文件已上传到FTP========"));
             }
         } catch (Exception e) {
             throw new BusinessException("附件上传失败!");
         }
     }
Copy after login

FtpClientFile工具类方法

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
 
import com.google.gson.JsonArray;
 
/**
 * ftp 上传文件
 * FTPClient commons.net 3.0.1版本
 * @author Lenovo
 *
 */
public class FtpClientFile {
    
    private static final String hostname = "10.xx.xx.xx" ;//FTP IP
    
    private static final int port = 21;//FTP 端口号
    
    private static final String username = "ftpName";//FTP 登录账号
   
    private static final String password = "ftpPsd"; //FTP 登录密码
    
    private static final String pathname = "/";//FTP 工作路径
    
    
    /**
     * 上传文件(可供Action/Controller层使用)
     * @param fileName 上传到FTP服务器后的文件名称
     * @param inputStream 输入文件流
     * @return
     */
     public static boolean uploadFile(String fileName,FileInputStream inputStream){
         
     boolean flag = false;
     FTPClient ftpClient = new FTPClient();
     //设置超时
     ftpClient.setConnectTimeout(60*60*1000);
     //设置编码
     ftpClient.setControlEncoding("UTF-8");
     try {
         //连接FTP服务器
         ftpClient.connect(hostname, port);
         //登录FTP服务器
         ftpClient.login(username, password);
         //是否成功登录FTP服务器    
         int replyCode = ftpClient.getReplyCode();
         if(!FTPReply.isPositiveCompletion(replyCode)){
             return flag;
             }
         System.out.println("===========登录FTP成功了==========");
         ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
         //切换路径 创建路径
         ftpClient.makeDirectory(pathname);
         ftpClient.changeWorkingDirectory(pathname);
         ftpClient.enterLocalPassiveMode();
         //设置缓冲
         ftpClient.setBufferSize(1024 * 1024 * 20);
         //保持连接
         ftpClient.setKeepAlive(true);
         boolean a = ftpClient.storeFile(new String(fileName.getBytes("utf-8"),"iso-8859-1"), inputStream);
         if(a){
             System.out.println("===========创建文件成功=============="+a);
                 String fileName2 = fileName+"AAA";
                 boolean status = ftpClient.rename(fileName, fileName2);
                 if(status)
                 System.out.println("===========修改文件名称成功=============="+status);
             }
         inputStream.close();
         ftpClient.logout();
         flag = true;
         } catch (Exception e) {
             e.printStackTrace();
         } finally{
             if(ftpClient.isConnected()){
                 try {
                     ftpClient.disconnect();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
         return flag;
         
     }
    
     
         
    /* public static void main(String[] args) {
         String originfilename = "C:\\Users\\Lenovo\\Desktop\\xx.txt";
         FileInputStream inputStream;
         try {
            inputStream = new FileInputStream(new File(originfilename));
            boolean a = uploadFile("xx.txt","/104/",inputStream);
            System.out.println("上传文件成功============"+a);
         } catch (FileNotFoundException e) {
            e.printStackTrace();
         }
    }*/
     
     
}
Copy after login

上传文件到FTP时 注意:是否有权限登录服务器/上传文件等操作。

默认在浏览器输入自己的ftp地址访问下看看 ftp://10.xx.xx.xx:端口号 登录看看

How to upload files to FTP in java

The above is the detailed content of How to upload files to FTP in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template