BookController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.sf.springboot.controller;
  2. import com.sf.springboot.dto.Result;
  3. import com.sf.springboot.entity.Book;
  4. import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
  5. import org.springframework.http.HttpHeaders;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.*;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import javax.servlet.http.HttpSessionActivationListener;
  12. import java.io.BufferedInputStream;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.IOException;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. //@Controller
  19. //@ResponseBody
  20. //组合注解 @RestController = @Controller + @ResponseBody
  21. @RestController
  22. public class BookController {
  23. /**
  24. * 添加图书
  25. * @return
  26. */
  27. @RequestMapping("/addBook")
  28. public Result addBook(Book book){
  29. System.out.println(book);
  30. return new Result();
  31. }
  32. /**
  33. * 添加图书
  34. * @return
  35. */
  36. @RequestMapping("/addBook2")
  37. public Result addBook2(@RequestBody Book book){
  38. System.out.println(book);
  39. return new Result();
  40. }
  41. /**
  42. * 更新图书
  43. * @return
  44. */
  45. @RequestMapping("/updateBook")
  46. public Result updateBook(Book book){
  47. System.out.println(book);
  48. return new Result();
  49. }
  50. /**
  51. * 更新图书
  52. * @return
  53. */
  54. @RequestMapping("/updateBook2")
  55. public Result updateBook2(@RequestBody Book book){
  56. System.out.println(book);
  57. return new Result();
  58. }
  59. /**
  60. * 删除图书
  61. * @param bookId
  62. * @return
  63. */
  64. @RequestMapping("/deleteBook")
  65. public Result deleteBook(Integer bookId){
  66. System.out.println(bookId);
  67. return new Result();
  68. }
  69. /**
  70. * 删除图书的方式二
  71. * @param book
  72. * @return
  73. */
  74. @RequestMapping("/deleteBook2")
  75. public Result deleteBook2(@RequestBody Book book){
  76. System.out.println(book.getBookId());
  77. return new Result();
  78. }
  79. @RequestMapping("/getBookList")
  80. public Result getBookList(){
  81. System.out.println("查询图书。。。");
  82. return new Result();
  83. }
  84. /**
  85. * 根据图书的id去查询图书的详情
  86. */
  87. @RequestMapping("/getBookInfo")
  88. public Result getBookInfo(Integer bookId){
  89. System.out.println(bookId);
  90. return new Result();
  91. }
  92. /**
  93. * 根据图书的id去查询图书的详情2
  94. */
  95. @RequestMapping("/getBookInfo2")
  96. public Result getBookInfo2(@RequestBody Book book){
  97. System.out.println(book.getBookId());
  98. return new Result();
  99. }
  100. /**
  101. * 单文件上传
  102. * @return
  103. */
  104. @RequestMapping("/uploadFile")
  105. public Object uploadFile(MultipartFile file) throws IOException {
  106. System.out.println("文件上传。。。"+file.getOriginalFilename());
  107. file.transferTo(new File("d:\\"+file.getOriginalFilename()));
  108. Map map = new HashMap();
  109. map.put("code",200);
  110. map.put("msg","OK");
  111. return map;
  112. }
  113. /**
  114. * 多文件上传
  115. * @return
  116. */
  117. @RequestMapping("/uploadFiles")
  118. public Object uploadFiles(MultipartFile[] file) throws IOException {
  119. for (MultipartFile multipartFile : file) {
  120. System.out.println("文件上传。。。"+multipartFile.getOriginalFilename());
  121. multipartFile.transferTo(new File("d:\\"+multipartFile.getOriginalFilename()));
  122. }
  123. Map map = new HashMap();
  124. map.put("code",200);
  125. map.put("msg","OK");
  126. return map;
  127. }
  128. /**
  129. * 文件下载
  130. */
  131. @GetMapping(value = "/downLoad")
  132. public ResponseEntity<byte[]> downLoad() throws IOException {
  133. System.out.println("文件下载...");
  134. //获取下载文件的输入流
  135. BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("d:\\123.jpg")));
  136. //创建下载缓冲区
  137. byte[] body = new byte[in.available()];
  138. //将输入流数据读入缓冲区
  139. in.read(body);
  140. //创建响应头
  141. HttpHeaders headers = new HttpHeaders();
  142. //构建文件名称
  143. String fileName="123.jpg";
  144. headers.add("Content-Disposition", "attachment;filename="+fileName);
  145. //创建响应状态码
  146. HttpStatus ok = HttpStatus.OK;
  147. ResponseEntity<byte[]> response = new ResponseEntity<>(body,headers,ok);
  148. return response;
  149. }
  150. }