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

时间:2017-11-07 11:21:25

标签: spring-boot

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

谢谢, R上。

2 个答案:

答案 0 :(得分:2)

我有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

答案 1 :(得分:1)

我发现这个例子适用于我的案例:

@Controller
@RequestMapping(value = "/")
public class HomeController {

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"webpage.html\"");
        InputStream inputStream = new FileInputStream(new File("C:\\MyWebPage\\webpage.html"));

        return outputStream -> {
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                outputStream.write(data, 0, nRead);
            }
            inputStream.close();
        };
    }
}