FTP inputstream下载测试成功
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.fjy</groupId>
|
<groupId>com.fjy</groupId>
|
||||||
<artifactId>spring</artifactId>
|
<artifactId>spring</artifactId>
|
||||||
<version>V2.10</version>
|
<version>V3.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>spring</name>
|
<name>spring</name>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.fjy.spring.exception.UserException;
|
|||||||
import com.fjy.spring.properties.ServerProperties;
|
import com.fjy.spring.properties.ServerProperties;
|
||||||
import com.fjy.spring.service.FileService;
|
import com.fjy.spring.service.FileService;
|
||||||
import com.fjy.spring.service.LogService;
|
import com.fjy.spring.service.LogService;
|
||||||
|
import com.fjy.spring.untils.FtpOperationUtil;
|
||||||
import com.fjy.spring.untils.FtpUtils;
|
import com.fjy.spring.untils.FtpUtils;
|
||||||
import com.fjy.spring.untils.LogUtil;
|
import com.fjy.spring.untils.LogUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -144,7 +145,6 @@ public class DownLoadController {
|
|||||||
|
|
||||||
@RequestMapping("/home/download/dodownloadftp")
|
@RequestMapping("/home/download/dodownloadftp")
|
||||||
public String downloadFromFTP(@RequestParam Integer fileId, HttpServletRequest request, HttpServletResponse response) {
|
public String downloadFromFTP(@RequestParam Integer fileId, HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
|
||||||
response.setContentType("text/html;charset=utf-8");
|
response.setContentType("text/html;charset=utf-8");
|
||||||
try {
|
try {
|
||||||
request.setCharacterEncoding("UTF-8");
|
request.setCharacterEncoding("UTF-8");
|
||||||
@@ -157,23 +157,27 @@ public class DownLoadController {
|
|||||||
TbFile file = new TbFile();
|
TbFile file = new TbFile();
|
||||||
file.setColfileid(fileId);
|
file.setColfileid(fileId);
|
||||||
TbFile tbFile = fileService.findFileById(file);
|
TbFile tbFile = fileService.findFileById(file);
|
||||||
//TbFile tbFile = fileService.findFile(file);
|
System.out.println("【file】"+tbFile.toString());
|
||||||
|
|
||||||
//System.out.println(tbFile.getColfilepath());
|
|
||||||
|
|
||||||
String ctxPath = tbFile.getColfilepath();
|
String ctxPath = tbFile.getColfilepath();
|
||||||
String downLoadPath = ctxPath;
|
String downLoadPath = ctxPath;
|
||||||
//String downLoadPath = ctxPath + tbFile.getColfilename();
|
|
||||||
//System.out.println(downLoadPath);
|
|
||||||
|
|
||||||
// FtpUtils.downFtpFile(ctxPath,tbFile.getColfilename(),)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
FtpOperationUtil ftpUtil = new FtpOperationUtil();
|
||||||
|
|
||||||
long fileLength = new File(downLoadPath).length();
|
long fileLength = new File(downLoadPath).length();
|
||||||
response.setContentType("application/x-msdownload;");
|
response.setContentType("application/x-msdownload;");
|
||||||
response.setHeader("Content-disposition", "attachment; filename=" + new String(tbFile.getColfilename().getBytes("utf-8"), "ISO8859-1"));
|
response.setHeader("Content-disposition", "attachment; filename=" + new String(tbFile.getColfilename().getBytes("utf-8"), "ISO8859-1"));
|
||||||
response.setHeader("Content-Length", String.valueOf(fileLength));
|
response.setHeader("Content-Length", String.valueOf(fileLength));
|
||||||
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
|
|
||||||
|
ftpUtil.connectServer("192.168.79.138", FTPClient.DEFAULT_PORT, "cms", "imis2", null);
|
||||||
|
List<String> list = ftpUtil.getFileList("/");
|
||||||
|
for (String str : list) {
|
||||||
|
System.out.println(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream inputStream = ftpUtil.downFile(new String(tbFile.getColfilename().getBytes("UTF-8"), "iso-8859-1"));
|
||||||
|
bis = new BufferedInputStream(inputStream);
|
||||||
bos = new BufferedOutputStream(response.getOutputStream());
|
bos = new BufferedOutputStream(response.getOutputStream());
|
||||||
byte[] buff = new byte[2048];
|
byte[] buff = new byte[2048];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
@@ -181,7 +185,7 @@ public class DownLoadController {
|
|||||||
bos.write(buff, 0, bytesRead);
|
bos.write(buff, 0, bytesRead);
|
||||||
}
|
}
|
||||||
// 记录下载日志
|
// 记录下载日志
|
||||||
addVisitLog("下载文件"+tbFile.getColrealname()+" "+tbFile.getColfilename());
|
// addVisitLog("下载文件" + tbFile.getColrealname() + " " + tbFile.getColfilename());
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -204,8 +208,8 @@ public class DownLoadController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class WebAppConfig implements WebMvcConfigurer {
|
public class WebAppConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
26
src/main/java/com/fjy/spring/properties/FtpProperties.java
Normal file
26
src/main/java/com/fjy/spring/properties/FtpProperties.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package com.fjy.spring.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-05-07 19:52
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "ftp")
|
||||||
|
@Data
|
||||||
|
public class FtpProperties {
|
||||||
|
//服务器地址名称
|
||||||
|
private String server;
|
||||||
|
//端口号
|
||||||
|
private int port;
|
||||||
|
//用户名称
|
||||||
|
private String username;
|
||||||
|
//密码
|
||||||
|
private String password;
|
||||||
|
//工作目录
|
||||||
|
private String location;
|
||||||
|
}
|
||||||
376
src/main/java/com/fjy/spring/untils/FtpOperationUtil.java
Normal file
376
src/main/java/com/fjy/spring/untils/FtpOperationUtil.java
Normal file
@@ -0,0 +1,376 @@
|
|||||||
|
package com.fjy.spring.untils;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fjy.spring.properties.FtpProperties;
|
||||||
|
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.FTPFileFilter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-05-07 19:56
|
||||||
|
*/
|
||||||
|
public class FtpOperationUtil {
|
||||||
|
|
||||||
|
private FTPClient ftpClient;
|
||||||
|
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
|
||||||
|
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 利用FtpProperties进行服务器连接
|
||||||
|
*
|
||||||
|
* @param FtpProperties 参数配置Bean类
|
||||||
|
* @throws SocketException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void connectServer(FtpProperties FtpProperties) throws SocketException,
|
||||||
|
IOException {
|
||||||
|
String server = FtpProperties.getServer();
|
||||||
|
int port = FtpProperties.getPort();
|
||||||
|
String user = FtpProperties.getUsername();
|
||||||
|
String password = FtpProperties.getPassword();
|
||||||
|
String location = FtpProperties.getLocation();
|
||||||
|
connectServer(server, port, user, password, location);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用详细信息进行服务器连接
|
||||||
|
*
|
||||||
|
* @param server:服务器地址名称
|
||||||
|
* @param port:端口号
|
||||||
|
* @param user:用户名
|
||||||
|
* @param password:用户密码
|
||||||
|
* @param path:转移到FTP服务器目录
|
||||||
|
* @throws SocketException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void connectServer(String server, int port, String user,
|
||||||
|
String password, String path) throws SocketException, IOException {
|
||||||
|
ftpClient = new FTPClient();
|
||||||
|
ftpClient.connect(server, port);
|
||||||
|
System.out.println("Connected to " + server + ".");
|
||||||
|
//连接成功后的回应码
|
||||||
|
System.out.println(ftpClient.getReplyCode());
|
||||||
|
ftpClient.login(user, password);
|
||||||
|
if (path != null && path.length() != 0) {
|
||||||
|
ftpClient.changeWorkingDirectory(path);
|
||||||
|
}
|
||||||
|
ftpClient.setBufferSize(1024);//设置上传缓存大小
|
||||||
|
ftpClient.setControlEncoding("UTF-8");//设置编码
|
||||||
|
ftpClient.setFileType(BINARY_FILE_TYPE);//设置文件类型
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置传输文件类型:FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
|
||||||
|
* 二进制文件或文本文件
|
||||||
|
*
|
||||||
|
* @param fileType
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void setFileType(int fileType) throws IOException {
|
||||||
|
ftpClient.setFileType(fileType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭连接
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void closeServer() throws IOException {
|
||||||
|
if (ftpClient != null && ftpClient.isConnected()) {
|
||||||
|
ftpClient.logout();//退出FTP服务器
|
||||||
|
ftpClient.disconnect();//关闭FTP连接
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转移到FTP服务器工作目录
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean changeDirectory(String path) throws IOException {
|
||||||
|
return ftpClient.changeWorkingDirectory(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在服务器上创建目录
|
||||||
|
*
|
||||||
|
* @param pathName
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean createDirectory(String pathName) throws IOException {
|
||||||
|
return ftpClient.makeDirectory(pathName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在服务器上删除目录
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean removeDirectory(String path) throws IOException {
|
||||||
|
return ftpClient.removeDirectory(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除所有文件和目录
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* @param isAll true:删除所有文件和目录
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean removeDirectory(String path, boolean isAll)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
|
if (!isAll) {
|
||||||
|
return removeDirectory(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
|
||||||
|
if (ftpFileArr == null || ftpFileArr.length == 0) {
|
||||||
|
return removeDirectory(path);
|
||||||
|
}
|
||||||
|
//
|
||||||
|
for (FTPFile ftpFile : ftpFileArr) {
|
||||||
|
String name = ftpFile.getName();
|
||||||
|
if (ftpFile.isDirectory()) {
|
||||||
|
System.out.println("* [sD]Delete subPath [" + path + "/" + name + "]");
|
||||||
|
removeDirectory(path + "/" + name, true);
|
||||||
|
} else if (ftpFile.isFile()) {
|
||||||
|
System.out.println("* [sF]Delete file [" + path + "/" + name + "]");
|
||||||
|
deleteFile(path + "/" + name);
|
||||||
|
} else if (ftpFile.isSymbolicLink()) {
|
||||||
|
|
||||||
|
} else if (ftpFile.isUnknown()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ftpClient.removeDirectory(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查目录在服务器上是否存在 true:存在 false:不存在
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean existDirectory(String path) throws IOException {
|
||||||
|
boolean flag = false;
|
||||||
|
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
|
||||||
|
for (FTPFile ftpFile : ftpFileArr) {
|
||||||
|
if (ftpFile.isDirectory()
|
||||||
|
&& ftpFile.getName().equalsIgnoreCase(path)) {
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isExistsFile(String fileName) {
|
||||||
|
try {
|
||||||
|
FTPFile[] file = ftpClient.listFiles(fileName);
|
||||||
|
return file.length > 0 ? true : false;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到文件列表,listFiles返回包含目录和文件,它返回的是一个FTPFile数组
|
||||||
|
* listNames():只包含目录的字符串数组
|
||||||
|
* String[] fileNameArr = ftpClient.listNames(path);
|
||||||
|
*
|
||||||
|
* @param path:服务器上的文件目录:/DF4
|
||||||
|
*/
|
||||||
|
public List<String> getFileList(String path) throws IOException {
|
||||||
|
FTPFile[] ftpFiles = ftpClient.listFiles(path);
|
||||||
|
//通过FTPFileFilter遍历只获得文件
|
||||||
|
/* FTPFile[] ftpFiles2= ftpClient.listFiles(path,new FTPFileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(FTPFile ftpFile) {
|
||||||
|
return ftpFile.isFile();
|
||||||
|
}
|
||||||
|
}); */
|
||||||
|
List<String> retList = new ArrayList<String>();
|
||||||
|
if (ftpFiles == null || ftpFiles.length == 0) {
|
||||||
|
return retList;
|
||||||
|
}
|
||||||
|
for (FTPFile ftpFile : ftpFiles) {
|
||||||
|
if (ftpFile.isFile()) {
|
||||||
|
retList.add(ftpFile.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除服务器上的文件
|
||||||
|
*
|
||||||
|
* @param pathName
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean deleteFile(String pathName) throws IOException {
|
||||||
|
return ftpClient.deleteFile(pathName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件到ftp服务器
|
||||||
|
* 在进行上传和下载文件的时候,设置文件的类型最好是:
|
||||||
|
* ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE)
|
||||||
|
* localFilePath:本地文件路径和名称
|
||||||
|
* remoteFileName:服务器文件名称
|
||||||
|
*/
|
||||||
|
public boolean uploadFile(String localFilePath, String remoteFileName)
|
||||||
|
throws IOException {
|
||||||
|
remoteFileName = new String(remoteFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||||
|
boolean flag = false;
|
||||||
|
InputStream iStream = null;
|
||||||
|
try {
|
||||||
|
iStream = new FileInputStream(localFilePath);
|
||||||
|
//我们可以使用BufferedInputStream进行封装
|
||||||
|
//BufferedInputStream bis=new BufferedInputStream(iStream);
|
||||||
|
//flag = ftpClient.storeFile(remoteFileName, bis);
|
||||||
|
flag = ftpClient.storeFile(remoteFileName, iStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
flag = false;
|
||||||
|
return flag;
|
||||||
|
} finally {
|
||||||
|
if (iStream != null) {
|
||||||
|
iStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件到ftp服务器,上传新的文件名称和原名称一样
|
||||||
|
*
|
||||||
|
* @param fileName:文件名称
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean uploadFile(String fileName) throws IOException {
|
||||||
|
return uploadFile(fileName, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件到ftp服务器
|
||||||
|
*
|
||||||
|
* @param iStream 输入流
|
||||||
|
* @param newName 新文件名称
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean uploadFile(InputStream iStream, String newName)
|
||||||
|
throws IOException {
|
||||||
|
boolean flag = false;
|
||||||
|
try {
|
||||||
|
flag = ftpClient.storeFile(newName, iStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
flag = false;
|
||||||
|
return flag;
|
||||||
|
} finally {
|
||||||
|
if (iStream != null) {
|
||||||
|
iStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从ftp服务器上下载文件到本地
|
||||||
|
*
|
||||||
|
* @param remoteFileName:ftp服务器上文件名称
|
||||||
|
* @param localFileName:本地文件名称
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public boolean download(String remoteFileName, String localFileName)
|
||||||
|
throws IOException {
|
||||||
|
remoteFileName = new String(remoteFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||||
|
|
||||||
|
boolean flag = false;
|
||||||
|
File outfile = new File(localFileName);
|
||||||
|
OutputStream oStream = null;
|
||||||
|
try {
|
||||||
|
oStream = new FileOutputStream(outfile);
|
||||||
|
//我们可以使用BufferedOutputStream进行封装
|
||||||
|
//BufferedOutputStream bos=new BufferedOutputStream(oStream);
|
||||||
|
//flag = ftpClient.retrieveFile(remoteFileName, bos);
|
||||||
|
flag = ftpClient.retrieveFile(remoteFileName, oStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
flag = false;
|
||||||
|
return flag;
|
||||||
|
} finally {
|
||||||
|
oStream.close();
|
||||||
|
}
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从ftp服务器上下载文件到本地
|
||||||
|
*
|
||||||
|
* @param sourceFileName:服务器资源文件名称
|
||||||
|
* @return InputStream 输入流
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public InputStream downFile(String sourceFileName) throws IOException {
|
||||||
|
sourceFileName = sourceFileName;
|
||||||
|
return ftpClient.retrieveFileStream(sourceFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
//testUpload();
|
||||||
|
//testDownload();
|
||||||
|
FtpOperationUtil ftpUtil = new FtpOperationUtil();
|
||||||
|
ftpUtil.connectServer("192.168.79.138", FTPClient.DEFAULT_PORT, "cms", "imis2", null);
|
||||||
|
//获得ftp服务器上目录名称为DF4下的所有文件名称
|
||||||
|
List<String> list = ftpUtil.getFileList("/");
|
||||||
|
for (String str : list) {
|
||||||
|
System.out.println(str);
|
||||||
|
}
|
||||||
|
// 上传本地D盘文件aaa.txt到服务器,服务器上名称为bbb.txt
|
||||||
|
//ftpUtil.uploadFile("F:\\JAVA Workspace\\Temp\\upload\\ERP实验1:销售预测与SOP.doc", "ERP实验1:销售预测与SOP.doc");
|
||||||
|
// 从服务器上下载文件bbb.txt到本地d盘名称为ccc.txt
|
||||||
|
//ftpUtil.download("ERP实验1:销售预测与SOP.doc", "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc");
|
||||||
|
|
||||||
|
String name = "ERP实验1:销售预测与SOP.doc";
|
||||||
|
InputStream inputStream = ftpUtil.downFile(new String(name.getBytes("UTF-8"), "iso-8859-1"));
|
||||||
|
|
||||||
|
String destination = "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc";
|
||||||
|
int index;
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
FileOutputStream downloadFile = new FileOutputStream(destination);
|
||||||
|
while ((index = inputStream.read(bytes)) != -1) {
|
||||||
|
downloadFile.write(bytes, 0, index);
|
||||||
|
downloadFile.flush();
|
||||||
|
}
|
||||||
|
downloadFile.close();
|
||||||
|
inputStream.close();
|
||||||
|
|
||||||
|
// 删除ftp服务器上文件:bbb.txt
|
||||||
|
//ftpUtil.deleteFile("bbb.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import org.apache.commons.net.ftp.FTPClient;
|
|||||||
import org.apache.commons.net.ftp.FTPFile;
|
import org.apache.commons.net.ftp.FTPFile;
|
||||||
import org.apache.commons.net.ftp.FTPReply;
|
import org.apache.commons.net.ftp.FTPReply;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,8 +13,8 @@ import java.io.*;
|
|||||||
* @date 2018-05-06 11:35
|
* @date 2018-05-06 11:35
|
||||||
*/
|
*/
|
||||||
public class FtpUtils {
|
public class FtpUtils {
|
||||||
private static final String FTP_URL = "176.122.138.235";
|
// private static final String FTP_URL = "176.122.138.235";
|
||||||
// private static final String FTP_URL = "192.168.79.138";
|
private static final String FTP_URL = "192.168.79.138";
|
||||||
private static final int PORT = 21;
|
private static final int PORT = 21;
|
||||||
private static final String USER_NAME = "cms";
|
private static final String USER_NAME = "cms";
|
||||||
private static final String PASSWORD = "imis2";
|
private static final String PASSWORD = "imis2";
|
||||||
@@ -40,13 +41,10 @@ public class FtpUtils {
|
|||||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||||
destName = new String(destName.getBytes("UTF-8"), "iso-8859-1");
|
destName = new String(destName.getBytes("UTF-8"), "iso-8859-1");
|
||||||
|
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
FileInputStream fis = null;
|
FileInputStream fis = null;
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
File srcFile = new File(srcFileName);
|
File srcFile = new File(srcFileName);
|
||||||
fis = new FileInputStream(srcFile);
|
fis = new FileInputStream(srcFile);
|
||||||
|
|
||||||
@@ -85,12 +83,9 @@ public class FtpUtils {
|
|||||||
* @param downloadName 下载后存储的文件名全路径
|
* @param downloadName 下载后存储的文件名全路径
|
||||||
*/
|
*/
|
||||||
public static boolean download(String directory, String destFileName, String downloadName) throws IOException {
|
public static boolean download(String directory, String destFileName, String downloadName) throws IOException {
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.setBufferSize(1024);
|
ftpClient.setBufferSize(1024);
|
||||||
|
|
||||||
// 设置文件类型(二进制)
|
// 设置文件类型(二进制)
|
||||||
@@ -119,11 +114,11 @@ public class FtpUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
|
||||||
* @param remotePath
|
* @param remotePath
|
||||||
* @param fileName
|
* @param fileName
|
||||||
* @param localPath
|
* @param localPath
|
||||||
* @return
|
* @return
|
||||||
|
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||||
*/
|
*/
|
||||||
public static boolean downFtpFile(String remotePath, String fileName,
|
public static boolean downFtpFile(String remotePath, String fileName,
|
||||||
String localPath) {
|
String localPath) {
|
||||||
@@ -171,15 +166,16 @@ public class FtpUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
|
||||||
* @param remotePath
|
|
||||||
* @param fileName
|
* @param fileName
|
||||||
* @param localPath
|
|
||||||
* @return
|
* @return
|
||||||
|
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||||
*/
|
*/
|
||||||
public static boolean downFtpFileByStream(String remotePath, String fileName,
|
public static OutputStream downFtpFileByStream(String fileName) {
|
||||||
String localPath) {
|
|
||||||
|
String remotePath = "/www/ftp/cms/";
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
|
OutputStream is = null;
|
||||||
|
|
||||||
FTPClient ftp = new FTPClient();
|
FTPClient ftp = new FTPClient();
|
||||||
try {
|
try {
|
||||||
int reply;
|
int reply;
|
||||||
@@ -190,7 +186,7 @@ public class FtpUtils {
|
|||||||
reply = ftp.getReplyCode();
|
reply = ftp.getReplyCode();
|
||||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||||
ftp.disconnect();
|
ftp.disconnect();
|
||||||
return success;
|
return null;
|
||||||
}
|
}
|
||||||
// 转移到FTP服务器目录
|
// 转移到FTP服务器目录
|
||||||
ftp.changeWorkingDirectory(remotePath);
|
ftp.changeWorkingDirectory(remotePath);
|
||||||
@@ -200,11 +196,10 @@ public class FtpUtils {
|
|||||||
for (FTPFile ff : fs) {
|
for (FTPFile ff : fs) {
|
||||||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||||||
if (fname.equals(fileName)) {
|
if (fname.equals(fileName)) {
|
||||||
File localFile = new File(localPath+fname);
|
File localFile = new File(fname);
|
||||||
OutputStream is = new FileOutputStream(localFile);
|
is = new FileOutputStream(localFile);
|
||||||
ftp.retrieveFile(ff.getName(), is);
|
ftp.retrieveFile(ff.getName(), is);
|
||||||
is.close();
|
return is;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ftp.logout();
|
ftp.logout();
|
||||||
@@ -212,6 +207,12 @@ public class FtpUtils {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
|
try {
|
||||||
|
assert is != null;
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
if (ftp.isConnected()) {
|
if (ftp.isConnected()) {
|
||||||
try {
|
try {
|
||||||
ftp.disconnect();
|
ftp.disconnect();
|
||||||
@@ -219,7 +220,7 @@ public class FtpUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return success;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -238,11 +239,8 @@ public class FtpUtils {
|
|||||||
* 判断远程文件是否重命名成功,如果成功返回true,否则返回false
|
* 判断远程文件是否重命名成功,如果成功返回true,否则返回false
|
||||||
*/
|
*/
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.changeWorkingDirectory(directory);
|
ftpClient.changeWorkingDirectory(directory);
|
||||||
//重命名远程文件
|
//重命名远程文件
|
||||||
result = ftpClient.rename(oldFileName, newFileName);
|
result = ftpClient.rename(oldFileName, newFileName);
|
||||||
@@ -274,11 +272,8 @@ public class FtpUtils {
|
|||||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||||
*/
|
*/
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.changeWorkingDirectory(directory);
|
ftpClient.changeWorkingDirectory(directory);
|
||||||
//删除远程文件
|
//删除远程文件
|
||||||
result = ftpClient.deleteFile(fileName);
|
result = ftpClient.deleteFile(fileName);
|
||||||
@@ -310,11 +305,8 @@ public class FtpUtils {
|
|||||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||||
*/
|
*/
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.changeWorkingDirectory(directory);
|
ftpClient.changeWorkingDirectory(directory);
|
||||||
result = ftpClient.makeDirectory(newDirectory);//创建新目录
|
result = ftpClient.makeDirectory(newDirectory);//创建新目录
|
||||||
return result;
|
return result;
|
||||||
@@ -345,11 +337,8 @@ public class FtpUtils {
|
|||||||
|
|
||||||
// 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
// 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.changeWorkingDirectory(directory);
|
ftpClient.changeWorkingDirectory(directory);
|
||||||
// 重命名目录
|
// 重命名目录
|
||||||
result = ftpClient.rename(oldDirectory, newDirectory);
|
result = ftpClient.rename(oldDirectory, newDirectory);
|
||||||
@@ -381,11 +370,8 @@ public class FtpUtils {
|
|||||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||||
*/
|
*/
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.changeWorkingDirectory(directory);
|
ftpClient.changeWorkingDirectory(directory);
|
||||||
result = ftpClient.removeDirectory(deldirectory);//删除目录
|
result = ftpClient.removeDirectory(deldirectory);//删除目录
|
||||||
return result;
|
return result;
|
||||||
@@ -408,17 +394,12 @@ public class FtpUtils {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static String[] list(String directory) throws IOException {
|
public static String[] list(String directory) throws IOException {
|
||||||
FTPClient ftpClient = new FTPClient();
|
FTPClient ftpClient = makeConnectionFactory();
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(FTP_URL, PORT);
|
|
||||||
ftpClient.login(USER_NAME, PASSWORD);
|
|
||||||
ftpClient.enterLocalPassiveMode();
|
|
||||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
|
||||||
ftpClient.changeWorkingDirectory(directory);
|
ftpClient.changeWorkingDirectory(directory);
|
||||||
ftpClient.enterLocalPassiveMode();
|
ftpClient.enterLocalPassiveMode();
|
||||||
//删除目录
|
//删除目录
|
||||||
String[] list = ftpClient.listNames();
|
return ftpClient.listNames();
|
||||||
return list;
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -431,4 +412,19 @@ public class FtpUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建FTP连接的工厂方法
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static FTPClient makeConnectionFactory() throws IOException {
|
||||||
|
FTPClient ftpClient = new FTPClient();
|
||||||
|
ftpClient.connect(FTP_URL, PORT);
|
||||||
|
ftpClient.login(USER_NAME, PASSWORD);
|
||||||
|
ftpClient.enterLocalPassiveMode();
|
||||||
|
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||||
|
return ftpClient;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ server:
|
|||||||
serverproperties:
|
serverproperties:
|
||||||
port_num: 8080
|
port_num: 8080
|
||||||
filePath: F:\JAVA Workspace\Temp\
|
filePath: F:\JAVA Workspace\Temp\
|
||||||
|
ftp:
|
||||||
|
server: 192.168.79.138
|
||||||
|
port: 21
|
||||||
|
username: cms
|
||||||
|
password: imis2
|
||||||
|
location: /home/ftp/cms/
|
||||||
spring:
|
spring:
|
||||||
thymeleaf:
|
thymeleaf:
|
||||||
prefix: classpath:/templates/
|
prefix: classpath:/templates/
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ server:
|
|||||||
serverproperties:
|
serverproperties:
|
||||||
port_num: 8085
|
port_num: 8085
|
||||||
filePath: /www/cmsfile/
|
filePath: /www/cmsfile/
|
||||||
|
ftp:
|
||||||
|
server: 176.122.138.235
|
||||||
|
port: 21
|
||||||
|
username: cms
|
||||||
|
password: imis2
|
||||||
|
location: /www/ftp/cms/
|
||||||
spring:
|
spring:
|
||||||
thymeleaf:
|
thymeleaf:
|
||||||
prefix: classpath:/templates/
|
prefix: classpath:/templates/
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.fjy.spring.untils;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class FtpOperationUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectServer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectServer1() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setFileType() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void closeServer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void changeDirectory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDirectory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeDirectory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeDirectory1() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void existDirectory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isExistsFile() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getFileList() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deleteFile() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadFile() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadFile1() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadFile2() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void download() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void downFile() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
public class FtpUtilsTest {
|
public class FtpUtilsTest {
|
||||||
private static final String destName = "ERP实验1:销售预测与SOP.doc";
|
private static final String destName = "ERP实验1:销售预测与SOP.doc";
|
||||||
private static final String directory = "/www/cmsftp/";
|
private static final String directory = "/www/ftp/cms/";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void upload() throws Exception{
|
public void upload() throws Exception{
|
||||||
@@ -43,7 +43,7 @@ public class FtpUtilsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void remove() throws Exception{
|
public void remove() throws Exception{
|
||||||
FtpUtils.remove(directory,destName);
|
FtpUtils.remove(directory,"新名称.doc");
|
||||||
list();
|
list();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user