123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package com.sf.springboot.controller;
- import com.sf.springboot.dto.Result;
- import com.sf.springboot.entity.Book;
- import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpSessionActivationListener;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- //@Controller
- //@ResponseBody
- //组合注解 @RestController = @Controller + @ResponseBody
- @RestController
- public class BookController {
- /**
- * 添加图书
- * @return
- */
- @RequestMapping("/addBook")
- public Result addBook(Book book){
- System.out.println(book);
- return new Result();
- }
- /**
- * 添加图书
- * @return
- */
- @RequestMapping("/addBook2")
- public Result addBook2(@RequestBody Book book){
- System.out.println(book);
- return new Result();
- }
- /**
- * 更新图书
- * @return
- */
- @RequestMapping("/updateBook")
- public Result updateBook(Book book){
- System.out.println(book);
- return new Result();
- }
- /**
- * 更新图书
- * @return
- */
- @RequestMapping("/updateBook2")
- public Result updateBook2(@RequestBody Book book){
- System.out.println(book);
- return new Result();
- }
- /**
- * 删除图书
- * @param bookId
- * @return
- */
- @RequestMapping("/deleteBook")
- public Result deleteBook(Integer bookId){
- System.out.println(bookId);
- return new Result();
- }
- /**
- * 删除图书的方式二
- * @param book
- * @return
- */
- @RequestMapping("/deleteBook2")
- public Result deleteBook2(@RequestBody Book book){
- System.out.println(book.getBookId());
- return new Result();
- }
- @RequestMapping("/getBookList")
- public Result getBookList(){
- System.out.println("查询图书。。。");
- return new Result();
- }
- /**
- * 根据图书的id去查询图书的详情
- */
- @RequestMapping("/getBookInfo")
- public Result getBookInfo(Integer bookId){
- System.out.println(bookId);
- return new Result();
- }
- /**
- * 根据图书的id去查询图书的详情2
- */
- @RequestMapping("/getBookInfo2")
- public Result getBookInfo2(@RequestBody Book book){
- System.out.println(book.getBookId());
- return new Result();
- }
- /**
- * 单文件上传
- * @return
- */
- @RequestMapping("/uploadFile")
- public Object uploadFile(MultipartFile file) throws IOException {
- System.out.println("文件上传。。。"+file.getOriginalFilename());
- file.transferTo(new File("d:\\"+file.getOriginalFilename()));
- Map map = new HashMap();
- map.put("code",200);
- map.put("msg","OK");
- return map;
- }
- /**
- * 多文件上传
- * @return
- */
- @RequestMapping("/uploadFiles")
- public Object uploadFiles(MultipartFile[] file) throws IOException {
- for (MultipartFile multipartFile : file) {
- System.out.println("文件上传。。。"+multipartFile.getOriginalFilename());
- multipartFile.transferTo(new File("d:\\"+multipartFile.getOriginalFilename()));
- }
- Map map = new HashMap();
- map.put("code",200);
- map.put("msg","OK");
- return map;
- }
- /**
- * 文件下载
- */
- @GetMapping(value = "/downLoad")
- public ResponseEntity<byte[]> downLoad() throws IOException {
- System.out.println("文件下载...");
- //获取下载文件的输入流
- BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("d:\\123.jpg")));
- //创建下载缓冲区
- byte[] body = new byte[in.available()];
- //将输入流数据读入缓冲区
- in.read(body);
- //创建响应头
- HttpHeaders headers = new HttpHeaders();
- //构建文件名称
- String fileName="123.jpg";
- headers.add("Content-Disposition", "attachment;filename="+fileName);
- //创建响应状态码
- HttpStatus ok = HttpStatus.OK;
- ResponseEntity<byte[]> response = new ResponseEntity<>(body,headers,ok);
- return response;
- }
- }
|