Java 直接读取本地磁盘的PDF文件很容易,但是要从FTP中获取文件数据缺需要使用工具来连接到FTP服务器,之后才能从中获取到相应的文件数据
操作系统类型:windows
开发工具: idea
项目管理:maven
需要的jar包
<!-- commons jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-digester3</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
执行程序
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
import java.io.InputStream;
/**
* Created with CosmosRay
*
* @author CosmosRay
* @date 2019/6/12
* Function:
*/
public class PDFTest {
public static void main(String[] args) {
//FTP服务器的IP
String ftpIp = "192.168.19.129";
//FTP服务器的端口
Integer ftpPort = 21;
//FTP服务器的用户名
String ftpName = "admin";
//FTP服务器的密码
String ftpPass = "admin";
//在FTP服务器中改查询的文件所在目录
String ftpDocument = "/";
byte[] b = new byte[512];
//文件名称
String fileName = "650000.pdf";
/**
* 登录ftp,获取事件
* 这里是ip,用户名,密码
*/
FTPClient ftp = PDFTest.ftp(ftpIp, ftpPort, ftpName, ftpPass);
if (null != ftp) {
try {
// 更改当前工作目录为文件所在的目录
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(ftpDocument);
// 从ftp上获取pdf目录下的文件
// FTPFile[] file = ftp.listFiles();
// 遍历所有文件,匹配需要查找的文件
int status = ftp.getReplyCode();
System.out.println("FTP status = " + status);
// 通过文件名称匹配到的文件流传入接口,转化成数组集合
InputStream in = ftp.retrieveFileStream(fileName);
int n = 0;
while ((n = in.read(b)) != -1) {
System.out.println("n = =:" + n);
// out.write(b, 0, n);
}
//将字节流传往jsp
// out.flush();
// in.close();
// out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 功能描述
*
* @return org.apache.commons.net.ftp.FTPClient
* @author CosmosRay
* @date 2019/6/12
*/
private static FTPClient ftp(String ip, int port, String user, String password) {
FTPClient ftpClient = new FTPClient();
try {
//连接服务器登录服务器
ftpClient.connect(ip, port);
ftpClient.login(user, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 是否成功登录服务器
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("connect failed...ftp:" + ip);
} else {
System.out.println("connect successfu...ftp:" + ip);
}
} catch (IOException e) {
e.printStackTrace();
}
if (!ftpClient.isConnected()) {
ftpClient = null;
}
return ftpClient;
}
}
输入内容
connect successfu...ftp服务器:192.168.19.129
FTP status = 250