app.yml配置:
ftp:
#ip
ip: 127.0.0.1
#端口
port: 21
#用户名
username: ftpadmin
#密码
password: xxxxxx
#初始化连接数
initialSize: 5
#编码格式
encoding: UTF-8
#缓冲区
bufferSize: 8192
#是否开启
isopen: true
#当获取ftp客户端失败后的重试次数
retryCount: 5
path: /home/ftpadmin/
服务器安装FTP,且设置账号密码,以及创建一个目录:/home/ftpadmin/ 主要存放文件
Controller控制层 主要获取查询以及文件流
@Scheduled(cron = "15,30,45 * * * * ?")
public void test() throws Exception {
// 创建一个excel文件
HSSFWorkbook book = new HSSFWorkbook();
// 创建Sheet对象
HSSFSheet sheet = book.createSheet("机构客户信息表");
// 创建excel文件标题
String[] tableHeader = {"创建人", "不稽查批次号", "角色ID", "角色List"};
HSSFRow row = null;
HSSFCell cell = null;
int rowIndex = 2;
row = sheet.createRow(1);
int i = 0;
for (String string : tableHeader) {
cell = row.createCell(i);
cell.setCellValue(string);
i++;
}
TJicWarningDetailVo tJicWarningDetailVo = new TJicWarningDetailVo();
List<TJicWarningDetail> list = tJicWarningSetupService.queryTJicWarningDetailss(tJicWarningDetailVo);
for (TJicWarningDetail organ : list) {
row = sheet.createRow(rowIndex);
cell = row.createCell(0);
cell.setCellValue(organ.getCreatename());
cell = row.createCell(1);
cell.setCellValue(organ.getNojichabatchno());
cell = row.createCell(2);
cell.setCellValue(organ.getRoletypelist());
cell = row.createCell(3);
cell.setCellValue(organ.getRoletypelistn());
rowIndex++;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
book.write(os);
byte[] b = os.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(b);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String name = new String(("jicharenwuzhongxin" + sdf.format(date) + ".xls").getBytes("GBK"), "iso-8859-1");//涉及到中文问题 根据系统实际编码改变
System.out.println("文件名称"+name);
boolean b1= ftpProcessor.uploadFile("/home/ftpadmin", name, in);
// boolean b2= ftpProcessor.uploadFile("/home/ftpadmin", name, in);
System.out.println("上传结果B1:"+b1);
Thread.sleep(1000);
boolean B3= ftpProcessor.downloadFile("/home/ftpadmin/", name,"E:/");
System.out.println("下载结果B3:"+B3);
Thread.sleep(1000);
}
pom.xml需导入jar包
<!--ftp文件上传-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!--自定义连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
FTP配置工具类:
FtpProperties.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "ftp")
public class FtpProperties {
private String ip;
private String port;
private String username;
private String password;
private Integer initialSize = 0;
private String encoding = "UTF-8";
private Integer bufferSize = 4096;
private Integer retryCount = 3;
}
FtpConstants.java格式接口
public interface FtpConstants {
//ftp文件路径编码格式
String DEFAULT_FTP_PATH_ENCODING="ISO-8859-1";
}
FtpClientPooledObjectFactory.java连接池工厂
import lombok.extern.slf4j.Slf4j;
import org
Spring boot实现查询列表list生成excel后,上传到FTP服务器(完整版)
最新推荐文章于 2022-07-29 22:31:17 发布
本文详细介绍了如何使用Spring Boot实现从列表数据生成Excel文件,并将生成的Excel文件完整上传到FTP服务器的过程,包括关键步骤和代码示例。
摘要由CSDN通过智能技术生成