JAVA - FTP上傳檔案
阿新 • • 發佈:2021-06-24
FTP上傳檔案工具類
package org.jeecg.modules.system.util; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import java.util.Vector; import com.jcraft.jsch.*; public class FtpUtils { /** * 連結sftp *@param host 主機 * @param port 埠 * @param username 使用者名稱 * @param password 密碼 * @return */ public static ChannelSftp connect(String host, int port, String username,String password) throws JSchException { ChannelSftp sftp = null; JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession= jsch.getSession(username, host, port); System.out.println("Session建立成功"); sshSession.setPassword(password); System.out.println("密碼輸入成功"); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); System.out.println("連結引數設定成功"); sshSession.setConfig(sshConfig); sshSession.connect(); System.out.println("Session已連線"); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; System.out.println("連線到主機" + host + "."); return sftp; } /** * 檔案重新命名 * @param directory 目錄 * @param oldname 舊檔名 * @param newname 新檔名 * @param sftp */ public static void renameFile(String directory, String oldname, String newname, ChannelSftp sftp) { try { sftp.cd(directory); sftp.rename(oldname, newname); } catch (Exception e) { e.printStackTrace(); } } /** * 檔案上傳 * @param directory 目錄 * @param uploadFile 要上傳的檔名 * @param sftp */ public static void upload(String directory, String uploadFile, ChannelSftp sftp) throws SftpException, FileNotFoundException { //url路徑上傳 // URL url = new URL(uploadFile); // URLConnection connection = url.openConnection(); // String sss = url.getFile(); sftp.cd(directory); File file = new File(uploadFile); Date dNow = new Date(); SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss"); sftp.put(new FileInputStream(file), ft.format(dNow) + file.getName()); // sftp.put(connection.getInputStream(), file.getName()); } public static void upload(String directory,String sftpFileName,InputStream input, ChannelSftp sftp) throws SftpException{ try { sftp.cd(directory); } catch (Exception e) { } sftp.put(input, sftpFileName); } /** * 檔案下載 * @param directory 目錄 * @param downloadFile 要下載檔名 * @param saveFile 保持的檔名 * @param sftp */ public static void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) { try { sftp.cd(directory); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } catch (Exception e) { e.printStackTrace(); } } /** * 檔案刪除 * @param directory 目錄 * @param deleteFile 要刪除的檔名 * @param sftp */ public static void delete(String directory, String deleteFile, ChannelSftp sftp) { try { sftp.cd(directory); sftp.rm(deleteFile); System.out.println("刪除成功"); } catch (Exception e) { System.out.println("刪除失敗"); e.printStackTrace(); } } /** * 列出目錄下的檔案 * @param directory 目錄 * @param sftp * @return * @throws SftpException */ public static Vector listFiles(String directory, ChannelSftp sftp) throws SftpException { return sftp.ls(directory); } //批量刪除檔案 public static void delete(String directory, String[] fileNames, ChannelSftp aa) { for (String fileName : fileNames) { delete(directory, fileName, aa); } } /** * 建立目錄資料夾 * @param directory 要建立資料夾的位置路徑 * @param fileName 要建立資料夾的名稱 * @param sftp sftp連線 */ public static void mkdir(String directory,String fileName,ChannelSftp sftp){ try { sftp.cd(directory); sftp.mkdir(fileName); System.out.println("資料夾建立成功"); } catch (Exception e) { System.out.println("資料夾建立失敗"); e.printStackTrace(); } } public static void main(String[] args) throws Exception{ Date dNow = new Date(); SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss"); String str = ft.format(dNow); System.out.println(str); // ChannelSftp cftp=FtpUtils.connect("101.32.253.149",22, "root", "Happywork666"); // FtpUtils.upload("/usr", "https://cdn.starmini.jp/mini-program-starpay-cloud-s3/2021/06/17/1165ddd7-6f06-401b-b747-d4c0e8389b1d.zip", cftp); } }
引數實體
package org.jeecg.modules.system.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import org.jeecgframework.poi.excel.annotation.Excel; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; import java.util.List; /** * @Description: 郵件通知配置 * @Author: jeecg-boot * @Date: 2021-03-10 * @Version: V1.0 */ @Data @Accessors(chain = true) @EqualsAndHashCode(callSuper = false) @ApiModel(value="gaia_ftp_config物件", description="ftp通知配置") public class GaiaFtpConfig implements Serializable { private static final long serialVersionUID = 1L; /**FTP伺服器地址*/ @Excel(name = "FTP伺服器地址", width = 15) @ApiModelProperty(value = "FTP伺服器地址") private String host; /**FTP伺服器埠*/ @Excel(name = "FTP伺服器埠", width = 15) @ApiModelProperty(value = "FTP伺服器埠") private Integer port; /**ftp使用者名稱*/ @Excel(name = "ftp使用者名稱", width = 15) @ApiModelProperty(value = "ftp使用者名稱") private String username; /**ftp密碼*/ @Excel(name = "ftp密碼", width = 15) @ApiModelProperty(value = "ftp密碼") private String password; /**儲存目錄*/ @Excel(name = "儲存目錄", width = 15) @ApiModelProperty(value = "儲存目錄") private String directory; /**檔案路徑*/ @Excel(name = "檔案路徑", width = 15) @ApiModelProperty(value = "檔案路徑") private List<String> attachment; }