|
@@ -1,14 +1,21 @@
|
|
|
package com.sf.conroller;
|
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
+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.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Controller
|
|
@@ -102,7 +109,102 @@ public class BookController {
|
|
|
@RequestMapping(value = "/testMap")
|
|
|
public String testMap(Map map){
|
|
|
/*向域对象中存数据*/
|
|
|
- map.put("userName","李雷Map");
|
|
|
+ map.put("userName","李雷");
|
|
|
return "result";
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试转发和重定向
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ @RequestMapping(value = "/testRedirect",method = RequestMethod.GET)
|
|
|
+ public String testRedirect(){
|
|
|
+ System.out.println("测试重定向处理.....");
|
|
|
+ /*
|
|
|
+ * redirect: 这是springmvc转发的关键字
|
|
|
+ * 默认情况下返回值为String类型时,返回值为视图名称
|
|
|
+ * 如果返回值以redirect作为前缀,那么redirect后面的变为请求地址
|
|
|
+ * */
|
|
|
+ return "redirect:ok";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping(value = "/testforward",method = RequestMethod.GET)
|
|
|
+ public String testforward(){
|
|
|
+ System.out.println("测试转发求处理.....");
|
|
|
+ /*
|
|
|
+ * forward: 这是springmvc转发的关键字
|
|
|
+ * 默认情况下返回值为String类型时,返回值为视图名称
|
|
|
+ * 如果返回值以forward作为前缀,那么forward后面的变为请求地址
|
|
|
+ * */
|
|
|
+ return "forward:ok";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/ok",method = RequestMethod.GET)
|
|
|
+ public String ok(){
|
|
|
+ System.out.println("ok.....");
|
|
|
+ return "result";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * w文件上传
|
|
|
+ */
|
|
|
+ @RequestMapping(value="/uploadFile")
|
|
|
+ public String uploadFile(@RequestParam("file") MultipartFile[] multipartFiles) throws IOException {
|
|
|
+// iter
|
|
|
+ for (MultipartFile multipartFile : multipartFiles) {
|
|
|
+ if(!multipartFile.isEmpty()){
|
|
|
+// 文件上传的方法
|
|
|
+ multipartFile.transferTo(new File("D:\\"+multipartFile.getOriginalFilename()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "result";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件下载
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/downloadFile")
|
|
|
+ public ResponseEntity<byte[]> downloadFile() throws IOException {
|
|
|
+// 确定文件位置
|
|
|
+ File file = new File("D://123.jpg");
|
|
|
+// 将要下载的文件变成输入流
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
+ //将输入流读到缓冲区
|
|
|
+ BufferedInputStream in = new BufferedInputStream(fileInputStream);
|
|
|
+ //创建按byte[] 的大小
|
|
|
+ byte[] body = new byte[in.available()];
|
|
|
+ in.read(body); //将输入流读到缓冲区
|
|
|
+ //文件名
|
|
|
+ String fileName="123.jpg";
|
|
|
+ //当文件名为中文时需要进行编码,否则会出现中文乱码
|
|
|
+ fileName= URLEncoder.encode(fileName, "UTF-8");
|
|
|
+ HttpHeaders header = new HttpHeaders();
|
|
|
+ //inline: 直接显示
|
|
|
+ //header.add("Content-Disposition", "inline;filename="+fileName);
|
|
|
+ //附件下载
|
|
|
+ header.add("Content-Disposition", "attachment;filename=123.jpg");
|
|
|
+ ResponseEntity<byte[]> result = new ResponseEntity<>(body,header,HttpStatus.OK);
|
|
|
+ in.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 单个异常处理
|
|
|
+// */
|
|
|
+// @ExceptionHandler(value = {Exception.class})
|
|
|
+// public ModelAndView testException(Exception e){
|
|
|
+// System.out.println("error--->"+e.getMessage());
|
|
|
+// ModelAndView modelAndView = new ModelAndView();
|
|
|
+// modelAndView.setViewName("error");
|
|
|
+// modelAndView.addObject("msg",e.getMessage());
|
|
|
+// return modelAndView;
|
|
|
+// }
|
|
|
+
|
|
|
}
|