FTP下载,本地测试通过
This commit is contained in:
@@ -8,8 +8,12 @@ import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FtpUtils;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -84,8 +88,8 @@ public class DownLoadController {
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
java.io.BufferedInputStream bis = null;
|
||||
java.io.BufferedOutputStream bos = null;
|
||||
BufferedInputStream bis = null;
|
||||
BufferedOutputStream bos = null;
|
||||
|
||||
TbFile file = new TbFile();
|
||||
file.setColfileid(fileId);
|
||||
@@ -138,6 +142,72 @@ public class DownLoadController {
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/home/download/dodownloadftp")
|
||||
public String downloadFromFTP(@RequestParam Integer fileId, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
try {
|
||||
request.setCharacterEncoding("UTF-8");
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
BufferedInputStream bis = null;
|
||||
BufferedOutputStream bos = null;
|
||||
|
||||
TbFile file = new TbFile();
|
||||
file.setColfileid(fileId);
|
||||
TbFile tbFile = fileService.findFileById(file);
|
||||
//TbFile tbFile = fileService.findFile(file);
|
||||
|
||||
//System.out.println(tbFile.getColfilepath());
|
||||
|
||||
String ctxPath = tbFile.getColfilepath();
|
||||
String downLoadPath = ctxPath;
|
||||
//String downLoadPath = ctxPath + tbFile.getColfilename();
|
||||
//System.out.println(downLoadPath);
|
||||
|
||||
// FtpUtils.downFtpFile(ctxPath,tbFile.getColfilename(),)
|
||||
|
||||
try {
|
||||
long fileLength = new File(downLoadPath).length();
|
||||
response.setContentType("application/x-msdownload;");
|
||||
response.setHeader("Content-disposition", "attachment; filename=" + new String(tbFile.getColfilename().getBytes("utf-8"), "ISO8859-1"));
|
||||
response.setHeader("Content-Length", String.valueOf(fileLength));
|
||||
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
|
||||
bos = new BufferedOutputStream(response.getOutputStream());
|
||||
byte[] buff = new byte[2048];
|
||||
int bytesRead;
|
||||
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
|
||||
bos.write(buff, 0, bytesRead);
|
||||
}
|
||||
//记录下载日志
|
||||
addVisitLog("下载文件"+tbFile.getColrealname()+" "+tbFile.getColfilename());
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (bis != null) {
|
||||
try {
|
||||
bis.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (bos != null) {
|
||||
try {
|
||||
bos.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入课程名和文件夹名称,打包下载目录下所有文件
|
||||
*
|
||||
@@ -217,8 +287,8 @@ public class DownLoadController {
|
||||
}
|
||||
}
|
||||
//将打包好的文件输出到客户端
|
||||
java.io.BufferedInputStream bis = null;
|
||||
java.io.BufferedOutputStream bos = null;
|
||||
BufferedInputStream bis = null;
|
||||
BufferedOutputStream bos = null;
|
||||
try {
|
||||
long fileLength = new File(desPath).length();
|
||||
response.setContentType("application/x-msdownload;");
|
||||
@@ -299,6 +369,48 @@ public class DownLoadController {
|
||||
}
|
||||
}*/
|
||||
|
||||
public void downloadConfigFile(HttpServletResponse response,@RequestParam("fileName")String fileName) {
|
||||
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("multipart/form-data");
|
||||
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
int reply;
|
||||
ftpClient.connect("120.120.120.156",21);
|
||||
ftpClient.login("user1", "user1");
|
||||
reply = ftpClient.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
ftpClient.disconnect();
|
||||
return;
|
||||
}
|
||||
ftpClient.changeWorkingDirectory("/GOS_CAS/BACKUP/cas_config_backup");//转移到FTP服务器目录
|
||||
FTPFile[] fs = ftpClient.listFiles();
|
||||
for(int i=0;i<fs.length;i++){
|
||||
if(fs[i].getName().equals(fileName)){
|
||||
String saveAsFileName = new String(fs[i].getName().getBytes("UTF-8"), "ISO8859-1");
|
||||
response.setHeader("Content-Disposition", "attachment;fileName="+saveAsFileName);
|
||||
OutputStream os = response.getOutputStream();
|
||||
ftpClient.retrieveFile(fs[i].getName(), os);
|
||||
os.flush();
|
||||
os.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
ftpClient.logout();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 登陆后的访问日志记录
|
||||
*
|
||||
|
||||
@@ -14,6 +14,9 @@ import com.fjy.spring.untils.GetIPAddrUtil;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -24,6 +27,8 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
434
src/main/java/com/fjy/spring/untils/FtpUtils.java
Normal file
434
src/main/java/com/fjy/spring/untils/FtpUtils.java
Normal file
@@ -0,0 +1,434 @@
|
||||
package com.fjy.spring.untils;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-05-06 11:35
|
||||
*/
|
||||
public class FtpUtils {
|
||||
private static final String FTP_URL = "176.122.138.235";
|
||||
// private static final String FTP_URL = "192.168.79.138";
|
||||
private static final int PORT = 21;
|
||||
private static final String USER_NAME = "cms";
|
||||
private static final String PASSWORD = "imis2";
|
||||
|
||||
/**
|
||||
* 本地字符编码
|
||||
*/
|
||||
private static final String LOCAL_CHARSET = "UTF-8";
|
||||
|
||||
/**
|
||||
* FTP协议里面,规定文件名编码为iso-8859-1
|
||||
*/
|
||||
private static String SERVER_CHARSET = "iso-8859-1";
|
||||
|
||||
/**
|
||||
* ftp上传单个文件
|
||||
*
|
||||
* @param directory 上传至ftp的路径名不包括ftp地址
|
||||
* @param srcFileName 要上传的文件全路径名
|
||||
* @param destName 上传至ftp后存储的文件名
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean upload(String directory, String srcFileName, String destName) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
destName = new String(destName.getBytes("UTF-8"),"iso-8859-1");
|
||||
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FileInputStream fis = null;
|
||||
boolean result = false;
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
File srcFile = new File(srcFileName);
|
||||
fis = new FileInputStream(srcFile);
|
||||
|
||||
// 设置上传目录
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
ftpClient.setBufferSize(1024);
|
||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||
|
||||
// 设置文件类型(二进制)
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
result = ftpClient.storeFile(destName, fis);
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("FTP端口配置错误:不是数字:");
|
||||
throw e;
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new FileNotFoundException();
|
||||
} catch (IOException e) {
|
||||
throw new IOException(e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(fis);
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* FTP单个文件下载
|
||||
*
|
||||
* @param directory 要下载的文件所在ftp的路径名不包含ftp地址
|
||||
* @param destFileName 要下载的文件名
|
||||
* @param downloadName 下载后存储的文件名全路径
|
||||
*/
|
||||
public static boolean download(String directory, String destFileName, String downloadName) throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
boolean result = false;
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setBufferSize(1024);
|
||||
|
||||
// 设置文件类型(二进制)
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
|
||||
System.out.println("destFileName:" + destFileName + ",downloadName:" + downloadName);
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(downloadName);
|
||||
result = ftpClient.retrieveFile(destFileName, fileOutputStream);
|
||||
|
||||
fileOutputStream.flush();
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new FileNotFoundException();
|
||||
} catch (IOException e) {
|
||||
throw new IOException(e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||
* @param remotePath
|
||||
* @param fileName
|
||||
* @param localPath
|
||||
* @return
|
||||
*/
|
||||
public static boolean downFtpFile(String remotePath, String fileName,
|
||||
String localPath) {
|
||||
boolean success = false;
|
||||
FTPClient ftp = new FTPClient();
|
||||
try {
|
||||
int reply;
|
||||
ftp.connect(FTP_URL, PORT);
|
||||
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
|
||||
// 登录
|
||||
ftp.login(USER_NAME, PASSWORD);
|
||||
reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
ftp.disconnect();
|
||||
return success;
|
||||
}
|
||||
// 转移到FTP服务器目录
|
||||
ftp.changeWorkingDirectory(remotePath);
|
||||
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
|
||||
FTPFile[] fs = ftp.listFiles();
|
||||
for (FTPFile ff : fs) {
|
||||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||||
if (fname.equals(fileName)) {
|
||||
File localFile = new File(localPath+fname);
|
||||
OutputStream is = new FileOutputStream(localFile);
|
||||
ftp.retrieveFile(ff.getName(), is);
|
||||
is.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
ftp.logout();
|
||||
success = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftp.isConnected()) {
|
||||
try {
|
||||
ftp.disconnect();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||
* @param remotePath
|
||||
* @param fileName
|
||||
* @param localPath
|
||||
* @return
|
||||
*/
|
||||
public static boolean downFtpFileByStream(String remotePath, String fileName,
|
||||
String localPath) {
|
||||
boolean success = false;
|
||||
FTPClient ftp = new FTPClient();
|
||||
try {
|
||||
int reply;
|
||||
ftp.connect(FTP_URL, PORT);
|
||||
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
|
||||
// 登录
|
||||
ftp.login(USER_NAME, PASSWORD);
|
||||
reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
ftp.disconnect();
|
||||
return success;
|
||||
}
|
||||
// 转移到FTP服务器目录
|
||||
ftp.changeWorkingDirectory(remotePath);
|
||||
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
|
||||
FTPFile[] fs = ftp.listFiles();
|
||||
for (FTPFile ff : fs) {
|
||||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||||
if (fname.equals(fileName)) {
|
||||
File localFile = new File(localPath+fname);
|
||||
OutputStream is = new FileOutputStream(localFile);
|
||||
ftp.retrieveFile(ff.getName(), is);
|
||||
is.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
ftp.logout();
|
||||
success = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftp.isConnected()) {
|
||||
try {
|
||||
ftp.disconnect();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param directory 要重命名的文件所在ftp的路径名不包含ftp地址
|
||||
* @param oldFileName 要重命名的文件名
|
||||
* @param newFileName 重命名后的文件名
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean rename(String directory, String oldFileName, String newFileName) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
oldFileName = new String(oldFileName.getBytes("UTF-8"),"iso-8859-1");
|
||||
newFileName = new String(newFileName.getBytes("UTF-8"),"iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否重命名成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
//重命名远程文件
|
||||
result = ftpClient.rename(oldFileName, newFileName);
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("连接ftp服务器失败!", e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param directory 要删除的文件所在ftp的路径名不包含ftp地址
|
||||
* @param fileName 要删除的文件名
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean remove(String directory, String fileName) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
//删除远程文件
|
||||
result = ftpClient.deleteFile(fileName);
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("连接ftp服务器失败!", e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param directory 要创建的目录所在ftp的路径名不包含ftp地址
|
||||
* @param newDirectory 要创建的新目录名
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean makeDirecotory(String directory, String newDirectory) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
newDirectory = new String(newDirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
result = ftpClient.makeDirectory(newDirectory);//创建新目录
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("连接ftp服务器失败!", e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param directory 要重命名的目录所在ftp的路径名不包含ftp地址
|
||||
* @param oldDirectory 要重命名的旧目录名
|
||||
* @param newDirectory 重命名后的新目录
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean renameDirecotory(String directory, String oldDirectory, String newDirectory) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
oldDirectory = new String(oldDirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
newDirectory = new String(newDirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
|
||||
// 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
// 重命名目录
|
||||
result = ftpClient.rename(oldDirectory, newDirectory);
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("连接ftp服务器失败!", e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param directory 要重命名的目录所在ftp的路径名不包含ftp地址
|
||||
* @param deldirectory 要删除的目录名
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean removeDirecotory(String directory, String deldirectory) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
deldirectory = new String(deldirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
result = ftpClient.removeDirectory(deldirectory);//删除目录
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("连接ftp服务器失败!", e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param directory
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String[] list(String directory) throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
//删除目录
|
||||
String[] list = ftpClient.listNames();
|
||||
return list;
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("连接ftp服务器失败!", e);
|
||||
} finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#控制配置文件调用
|
||||
spring:
|
||||
profiles:
|
||||
active: prod
|
||||
active: dev
|
||||
@@ -25,8 +25,8 @@
|
||||
<!--滚动策略-->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!--路径文件名,文件名包含时间-->
|
||||
<!--<fileNamePattern>F:\JAVA Workspace\Temp\log\%d\info.%d.log</fileNamePattern>-->
|
||||
<fileNamePattern>/www/cmsfile/%d/info.%d.log</fileNamePattern>
|
||||
<fileNamePattern>F:\JAVA Workspace\Temp\log\%d\info.%d.log</fileNamePattern>
|
||||
<!--<fileNamePattern>/www/cmsfile/%d/info.%d.log</fileNamePattern>-->
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
@@ -44,8 +44,8 @@
|
||||
<!--滚动策略-->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!--路径文件名,文件名包含时间-->
|
||||
<!--<fileNamePattern>F:\JAVA Workspace\Temp\log\error.%d.log</fileNamePattern>-->
|
||||
<fileNamePattern>/www/cmsfile/%d/error.%d.log</fileNamePattern>
|
||||
<fileNamePattern>F:\JAVA Workspace\Temp\log\error.%d.log</fileNamePattern>
|
||||
<!--<fileNamePattern>/www/cmsfile/%d/error.%d.log</fileNamePattern>-->
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
|
||||
83
src/test/java/com/fjy/spring/untils/FtpUtilsTest.java
Normal file
83
src/test/java/com/fjy/spring/untils/FtpUtilsTest.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package com.fjy.spring.untils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class FtpUtilsTest {
|
||||
private static final String destName = "ERP实验1:销售预测与SOP.doc";
|
||||
private static final String directory = "/www/cmsftp/";
|
||||
|
||||
@Test
|
||||
public void upload() throws Exception{
|
||||
String srcFileName = "F:\\JAVA Workspace\\Temp\\upload\\ERP实验1:销售预测与SOP.doc";
|
||||
FtpUtils.upload(directory,srcFileName,destName);
|
||||
list();
|
||||
}
|
||||
|
||||
/**
|
||||
* WIN10下载文件为0kb,请勿使用
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void download() throws Exception{
|
||||
String downloadName = "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc";
|
||||
|
||||
FtpUtils.download(directory,destName,downloadName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void downFtpFile() throws Exception{
|
||||
String downloadName = "F:\\JAVA Workspace\\Temp\\";
|
||||
boolean res = FtpUtils.downFtpFile(directory,destName,downloadName);
|
||||
System.out.println(res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rename() throws Exception{
|
||||
String oldFileName = destName;
|
||||
String newFileName = "新名称.doc";
|
||||
FtpUtils.rename(directory,oldFileName, newFileName);
|
||||
list();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove() throws Exception{
|
||||
FtpUtils.remove(directory,destName);
|
||||
list();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void makeDirecotory() throws Exception{
|
||||
String newDirectory = "test";
|
||||
String newDirectoryZH = "新目录";
|
||||
FtpUtils.makeDirecotory(directory,newDirectory);
|
||||
FtpUtils.makeDirecotory(directory,newDirectoryZH);
|
||||
list();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renameDirecotory() throws Exception{
|
||||
String oldDirectoryZH = "新目录";
|
||||
String newDirectoryZH = "重命名目录";
|
||||
FtpUtils.renameDirecotory(directory,oldDirectoryZH,newDirectoryZH);
|
||||
list();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDirecotory() throws Exception{
|
||||
String newDirectory = "test";
|
||||
String newDirectoryZH = "重命名目录";
|
||||
FtpUtils.removeDirecotory(directory,newDirectory);
|
||||
FtpUtils.removeDirecotory(directory,newDirectoryZH);
|
||||
list();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void list() throws Exception{
|
||||
String[] lists = FtpUtils.list(directory);
|
||||
for (String list : lists){
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user