gpt4 book ai didi

spring-boot - Spring Boot示例如何从服务器下载文件

转载 作者:行者123 更新时间:2023-12-01 11:18:33 26 4
gpt4 key购买 nike

我有一个 Spring Boot Web 应用程序,我需要一个如何从服务器下载文件的示例。

谢谢,R.

最佳答案

我有 this SpringBoot的运行项目。下面是输出 xlsx 文件的部分代码。

WebController.java

@RequestMapping(value = {"/excel"}, method = RequestMethod.GET)

public void excel(HttpServletResponse response, @RequestParam("email") String email) {

try {
Query query = new Query();
query.addCriteria(Criteria.where("email").is(email));

if(email.equals(""))
query=new Query();

List<MQTT_Server_Detail> list = mongoTemplate.find(query, MQTT_Server_Detail.class, "owner");

response.addHeader("Content-disposition", "attachment; filename=Door.xlsx");
response.setContentType("application/vnd.ms-excel");

Workbook workbook = new XSSFWorkbook();
workbook.createSheet("owner");
workbook.setSheetName(0, "Owner");
Sheet sheet = workbook.getSheetAt(0);

sheet.createRow(0);
sheet.getRow(0).createCell(0).setCellValue("Owner Email");
sheet.getRow(0).createCell(1).setCellValue("Topic");
sheet.getRow(0).createCell(2).setCellValue("Device Name");
sheet.getRow(0).createCell(3).setCellValue("Device ID");

Row row;
int num = 1;

for (MQTT_Server_Detail a : list) {
Devices devices = getDevice(a.getEmail());

for (int i = 0; i < devices.getDevicesID().size(); i++) {
row = sheet.createRow(num++);
row.createCell(0).setCellValue(a.getEmail());
row.createCell(1).setCellValue(a.getTopic());
row.createCell(2).setCellValue((String) devices.getDevicesName().get(i));
row.createCell(3).setCellValue((String) devices.getDevicesID().get(i));
}

}

sheet = workbook.createSheet("Users");

row = sheet.createRow(0);
row.createCell(0).setCellValue("Name");
row.createCell(1).setCellValue("Device");
row.createCell(2).setCellValue("CardID");
row.createCell(3).setCellValue("Email");
row.createCell(4).setCellValue("Mobile");
row.createCell(5).setCellValue("Blocked");
row.createCell(6).setCellValue("Last in Date");
row.createCell(7).setCellValue("Last in Time");
row.createCell(8).setCellValue("Last out Date");
row.createCell(9).setCellValue("Last out Time");
row.createCell(10).setCellValue("Owner");

Criteria criteria[] = new Criteria[list.size()];

for (int i = 0; i < list.size(); i++) {
criteria[i] = Criteria.where("owner").is(list.get(i).getEmail());
}

List<Users_POJO> users_pojoList;

if (list.size() == 0)
users_pojoList = new ArrayList<>();
else
users_pojoList = mongoTemplate.find(new Query().addCriteria(new Criteria().orOperator(criteria)),
Users_POJO.class, "users");


for (int i = 0; i < users_pojoList.size(); i++) {
row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(users_pojoList.get(i).getName());
row.createCell(1).setCellValue(users_pojoList.get(i).getDevice());
row.createCell(2).setCellValue(users_pojoList.get(i).getCard_id());
row.createCell(3).setCellValue(users_pojoList.get(i).getEmail());
row.createCell(4).setCellValue(users_pojoList.get(i).getMobile());
row.createCell(5).setCellValue(users_pojoList.get(i).getBlocked());
row.createCell(6).setCellValue(users_pojoList.get(i).getLast_in_date());
row.createCell(7).setCellValue(users_pojoList.get(i).getLast_in_time());
row.createCell(8).setCellValue(users_pojoList.get(i).getLast_out_date());
row.createCell(9).setCellValue(users_pojoList.get(i).getLast_out_time());
row.createCell(10).setCellValue(users_pojoList.get(i).getOwner());

}

sheet = workbook.createSheet("Logs");
row = sheet.createRow(0);
row.createCell(0).setCellValue("CardID");
row.createCell(1).setCellValue("Device");
row.createCell(2).setCellValue("Date");
row.createCell(3).setCellValue("Time");
row.createCell(4).setCellValue("Count");

criteria = new Criteria[users_pojoList.size()];
for (int i = 0; i < users_pojoList.size(); i++) {
criteria[i] = Criteria.where("card_id").is(users_pojoList.get(i).getCard_id());
}
query = new Query();
query.addCriteria(new Criteria().orOperator(criteria));

List<Log_POJO> log_pojoList;

if (users_pojoList.size() == 0)
log_pojoList = new ArrayList<>();
else
log_pojoList = mongoTemplate.find(query, Log_POJO.class, "logs");


for (int i = 0; i < log_pojoList.size(); i++) {
row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(log_pojoList.get(i).getCard_id());
row.createCell(1).setCellValue(log_pojoList.get(i).getDevice());

String date = log_pojoList.get(i).getDay() + "-" + log_pojoList.get(i).getMonth() + "-" + log_pojoList.get(i).getYear();
row.createCell(2).setCellValue(date);

String time = log_pojoList.get(i).getHour() + "-" + log_pojoList.get(i).getMin() + "-" + log_pojoList.get(i).getSec();
row.createCell(3).setCellValue(time);

row.createCell(4).setCellValue(log_pojoList.get(i).getSerial_no());

}
workbook.write(response.getOutputStream());

response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}

}

简而言之,你必须这样做:

response.addHeader("Content-disposition", "attachment; filename=Door.xlsx");
response.setContentType("application/vnd.ms-excel");
//get the outputstream of response and write data to it

关于spring-boot - Spring Boot示例如何从服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com