diff --git a/Spring-ch13-Response/pom.xml b/Spring-ch13-Response/pom.xml index f7d62a3..0a551e3 100644 --- a/Spring-ch13-Response/pom.xml +++ b/Spring-ch13-Response/pom.xml @@ -41,6 +41,15 @@ ${basedir}/web + + org.apache.maven.plugins + maven-compiler-plugin + + 21 + 21 + --enable-preview + + diff --git a/Spring-ch13-Response/src/main/java/com/ycjy/web/Download.java b/Spring-ch13-Response/src/main/java/com/ycjy/web/Download.java new file mode 100644 index 0000000..cf88b85 --- /dev/null +++ b/Spring-ch13-Response/src/main/java/com/ycjy/web/Download.java @@ -0,0 +1,25 @@ +package com.ycjy.web; + +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +@Controller +public class Download { + @GetMapping("/download") + public ResponseEntity download() throws IOException { + Path path = Paths.get("Z:/Edg浏览器默认下载位/logo.jpg"); + byte[] bytes = Files.readAllBytes(path); + + return ResponseEntity.ok() + .header("Content-Disposition", "attachment; filename=logo.jpg") + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(bytes); + } +} diff --git a/Spring-ch13-Response/src/main/java/com/ycjy/web/RequestView.java b/Spring-ch13-Response/src/main/java/com/ycjy/web/RequestView.java index d6778f3..8199cdb 100644 --- a/Spring-ch13-Response/src/main/java/com/ycjy/web/RequestView.java +++ b/Spring-ch13-Response/src/main/java/com/ycjy/web/RequestView.java @@ -1,11 +1,18 @@ package com.ycjy.web; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Map; @Controller