zoukankan      html  css  js  c++  java
  • FTP上传心得

    最近出了一个问题就是在本地上传FTP没有一点问题 可是部署到服务器上。上传的时候总是false。解决办法

    ftp.enterLocalPassiveMode();
    boolean storeFile = ftp.storeFile(filename, input);
    开启FTP被动传输
    package com.my.blog.website.utils;


    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPReply;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.io.*;

    /**
    * JAVA FTPClient 工具类
    *
    * commons-net-1.4.1.jar PFTClinet jar包
    *
    * @author : hpp
    */
    public class FtpClientUtils{

    private static final Logger log = LoggerFactory.getLogger(FtpClientUtils.class);

    /**
    * Description: 向FTP服务器上传文件
    * @Version1.0
    * @param url FTP服务器hostname
    * @param port FTP服务器端口
    * @param username FTP登录账号
    * @param password FTP登录密码
    * @param path FTP服务器保存目录
    * @param filename 上传到FTP服务器上的文件名
    * @param input 输入流
    * @return 成功返回true,否则返回false
    */
    public static boolean uploadFile(
    String url,//FTP服务器hostname
    int port,//FTP服务器端口
    String username, // FTP登录账号
    String password, //FTP登录密码
    String path, //FTP服务器保存目录
    String filename, //上传到FTP服务器上的文件名
    InputStream input // 输入流
    ) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
    log.info("FTP开始上传,文件名为:"+filename);
    int reply;
    ftp.connect(url, port);//连接FTP服务器
    //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    boolean login = ftp.login(username, password);//登录
    if(!login)
    log.info("ftp!!!!login失败");
    reply = ftp.getReplyCode();
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    log.info("FTP服务器 拒绝连接");
    return success;
    }
    boolean change = ftp.changeWorkingDirectory(path);

    ftp.enterLocalPassiveMode();
    boolean storeFile = ftp.storeFile(filename, input);
    input.close();
    ftp.logout();
    if(storeFile)
    log.info("FTP开上传成功!文件名为:"+filename);
    success = true;
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (ftp.isConnected()) {
    try {
    ftp.disconnect();
    } catch (IOException ioe) {
    }
    }
    }
    return success;
    }

    /**
    * 删除文件
    * @param fileName 要删除的文件地址
    * @return true/false
    * @throws IOException
    */
    public static boolean delete(String fileName, FTPClient ftpClient) throws IOException {
    return ftpClient.deleteFile(fileName);
    }


    /**
    * 下载文件到指定目录
    * @param ftpFile 文件服务器上的文件地址
    * @param dstFile 输出文件的路径和名称
    * @throws Exception
    */
    public static void downLoad(String ftpFile, String dstFile, FTPClient ftpClient) throws Exception {
    if (StringUtils.isBlank(ftpFile)) {
    throw new RuntimeException("ftpFile为空");
    }
    if (StringUtils.isBlank(dstFile)) {
    throw new RuntimeException("dstFile为空");
    }
    File file = new File(dstFile);
    FileOutputStream fos = new FileOutputStream(file);
    ftpClient.retrieveFile(ftpFile, fos);
    fos.flush();
    fos.close();
    }

    /**
    * 从文件服务器获取文件流
    * @param ftpFile 文件服务器上的文件地址
    * @return {@link InputStream}
    * @throws IOException
    */
    public static InputStream retrieveFileStream(String ftpFile, FTPClient ftpClient) throws IOException {
    if (StringUtils.isBlank(ftpFile)) {
    throw new RuntimeException("ftpFile为空");
    }
    return ftpClient.retrieveFileStream(ftpFile);
    }


    public static void main(String[] args) {
    try {
    FileInputStream in=new FileInputStream(new File("E:\qrcode.jpg"));
    boolean flag = uploadFile("139.199.116.44", 21, "zdz69824436", "j67fBybHFS", "/autoCar/", "qrcode.jpg", in);
    System.out.println(flag);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }
  • 相关阅读:
    实战-百度云[大文件/文件夹]下载限制破解
    IOCP之客户端及消息传递
    IOCP简单实现
    Charles V4系列更新 | 绿色特别版 | 视频教程
    Charles 3.11.5 绿色特别版
    VC运行库合集2005/2008/2010/2012/2013/2015
    手游测试之《弱网测试》
    后端性能测试不可不知的二三事
    linux性能指标及分析工具
    Shell笔记-04
  • 原文地址:https://www.cnblogs.com/java-xz/p/9520591.html
Copyright © 2011-2022 走看看