package com.qiwu.chatbotbackend.controller; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import static com.qiwu.chatbotbackend.Config.Config.BOT_BACKUP_DIR; @RestController @ResponseBody public class BackupController { @Autowired ResourceLoader loader; @GetMapping("/sayhi2") public ResponseEntity getFile(@PathVariable("name") String name){ System.out.println("---" + name); String filePath = BOT_BACKUP_DIR + name; byte[] body = null; try { body = IOUtils.toByteArray(loader.getResource("file:" + filePath).getInputStream()); }catch (Exception e){ e.printStackTrace(); } String fileName = filePath.substring(filePath.lastIndexOf('/')+1, filePath.length()); HttpHeaders headers=new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename="+fileName); return new ResponseEntity(body, headers, HttpStatus.OK); } @RequestMapping("/sayhi1") public ResponseEntity getFile2(){ System.out.println("---"); String filePath = BOT_BACKUP_DIR; byte[] body = null; try { body = IOUtils.toByteArray(loader.getResource("file:" + filePath).getInputStream()); }catch (Exception e){ e.printStackTrace(); } String fileName = filePath.substring(filePath.lastIndexOf('/')+1, filePath.length()); HttpHeaders headers=new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename="+fileName); return new ResponseEntity(body, headers, HttpStatus.OK); } }