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

java实现上传和下载工具类

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

java实现上传和下载工具类

本文实例为大家分享了文件上传到ftp服务工具类,供大家参考,具体内容如下

直接引用此java工具类就好

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FtpUtil {

   
  public static boolean uploadFile(String host, int port, String username, String password, String basePath,
      String filePath, String filename, InputStream input) {
    boolean result = false;
    FTPClient ftp = new FTPClient();
    try {
      int reply;
      ftp.connect(host, port);// 连接FTP服务器
      // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
      ftp.login(username, password);// 登录
      reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
 ftp.disconnect();
 return result;
      }
      //切换到上传目录
      if (!ftp.changeWorkingDirectory(basePath+filePath)) {
 //如果目录不存在创建目录
 String[] dirs = filePath.split("/");
 String tempPath = basePath;
 for (String dir : dirs) {
   if (null == dir || "".equals(dir)) continue;
   tempPath += "/" + dir;
   if (!ftp.changeWorkingDirectory(tempPath)) {
     if (!ftp.makeDirectory(tempPath)) {
return result;
     } else {
ftp.changeWorkingDirectory(tempPath);
     }
   }
 }
      }
      //设置上传文件的类型为二进制类型
      ftp.setFileType(FTP.BINARY_FILE_TYPE);
      //上传文件
      if (!ftp.storeFile(filename, input)) {
 return result;
      }
      input.close();
      ftp.logout();
      result = true;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (ftp.isConnected()) {
 try {
   ftp.disconnect();
 } catch (IOException ioe) {
 }
      }
    }
    return result;
  }
  
   
  public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
      String fileName, String localPath) {
    boolean result = false;
    FTPClient ftp = new FTPClient();
    try {
      int reply;
      ftp.connect(host, port);
      // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
      ftp.login(username, password);// 登录
      reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
 ftp.disconnect();
 return result;
      }
      ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
      FTPFile[] fs = ftp.listFiles();
      for (FTPFile ff : fs) {
 if (ff.getName().equals(fileName)) {
   File localFile = new File(localPath + "/" + ff.getName());

   OutputStream is = new FileOutputStream(localFile);
   ftp.retrieveFile(ff.getName(), is);
   is.close();
 }
      }

      ftp.logout();
      result = true;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (ftp.isConnected()) {
 try {
   ftp.disconnect();
 } catch (IOException ioe) {
 }
      }
    }
    return result;
  }
  
  public static void main(String[] args) {
    try { 
      FileInputStream in=new FileInputStream(new File("D:\temp\image\gaigeming.jpg")); 
      boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in); 
      System.out.println(flag); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } 
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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