12 changed files with 81 additions and 105 deletions
@ -1,71 +0,0 @@ |
|||||
package com.base.springcloud.util; |
|
||||
|
|
||||
import org.apache.commons.net.ftp.FTPClient; |
|
||||
import org.apache.commons.net.ftp.FTPReply; |
|
||||
import org.slf4j.Logger; |
|
||||
import org.slf4j.LoggerFactory; |
|
||||
import org.springframework.beans.factory.annotation.Value; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
import java.io.IOException; |
|
||||
import java.io.InputStream; |
|
||||
|
|
||||
@Component |
|
||||
public class FtpUtil { |
|
||||
|
|
||||
Logger logger = LoggerFactory.getLogger(FtpUtil.class); |
|
||||
|
|
||||
//ftp服务器ip地址
|
|
||||
@Value("${ftp.ftp_ip}") |
|
||||
private String FTP_ADDRESS; |
|
||||
//端口号
|
|
||||
@Value("${ftp.ftp_prot}") |
|
||||
private int FTP_PORT; |
|
||||
//用户名
|
|
||||
@Value("${ftp.ftp_username}") |
|
||||
private String FTP_USERNAME; |
|
||||
//密码
|
|
||||
@Value("${ftp.ftp_password}") |
|
||||
private String FTP_PASSWORD; |
|
||||
|
|
||||
//路径都是/home/加上用户名
|
|
||||
@Value("${ftp.ftp_basepath}") |
|
||||
public String FTP_BASEPATH; |
|
||||
|
|
||||
|
|
||||
// 文件上传
|
|
||||
public boolean uploadFile(String originFileName, InputStream input) { |
|
||||
//这是最开始引入的依赖里的方法
|
|
||||
FTPClient ftp = new FTPClient(); |
|
||||
ftp.setControlEncoding("utf-8"); |
|
||||
try { |
|
||||
int reply; |
|
||||
ftp.connect(FTP_ADDRESS, FTP_PORT);// 连接FTP服务器
|
|
||||
ftp.login(FTP_USERNAME, FTP_PASSWORD);// 登录
|
|
||||
reply = ftp.getReplyCode();//连接成功会的到一个返回状态码
|
|
||||
System.out.println(reply);//可以输出看一下是否连接成功
|
|
||||
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
|
|
||||
ftp.changeWorkingDirectory(FTP_BASEPATH);//修改操作空间
|
|
||||
//对了这里说明一下你所操作的文件夹必须要有可读权限,chomd 777 文件夹名//这里我就是用的我的home文件夹
|
|
||||
ftp.storeFile(originFileName, input); |
|
||||
if (!FTPReply.isPositiveCompletion(reply)) { |
|
||||
logger.warn("ftp connect fail...."); |
|
||||
ftp.disconnect(); |
|
||||
return false; |
|
||||
} |
|
||||
input.close(); |
|
||||
ftp.logout(); |
|
||||
return true; |
|
||||
}catch (Exception e){ |
|
||||
e.printStackTrace(); |
|
||||
}finally { |
|
||||
if (ftp.isConnected()) { |
|
||||
try { |
|
||||
ftp.disconnect(); |
|
||||
} catch (IOException ioe) { |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue