2021SC@SDUSC
filecontroller分析:
该类添加了@RestController注解,在Spring中@RestController的作用等同于@Controller + @ResponseBody。
该类中每个方法前都有一个@RequestMapping注解,这个注解是用来映射请求的,即指明处理器可以处理哪些URL请求,该注解既可以用在类上,也可以用在方法上。相当于Servlet中在web.xml中做如下配置。
当使用该注解标记控制器类时,方法的请求地址是相对类的请求地址而言的;当没有使用@RequestMapping标记类时,方法的请求地址是绝对路径。@RequestMapping的地址可以是url变量,通过@PathVariable注解获取作为方法的参数。也可以是通配符来筛选请求地址。
当@RequestMapping(value = "xxx")注解在方法上时,value属性指定了该方法可以处理的 URL 请求路径则是 http://localhost/SpringMVC/xxx,如本类中,fileUpload()添加了@RequestMapping(value = "fileUpload", method = RequestMethod.POST),可以处理的URL请求路径则是http://localhost/SpringMVC/fileUpload。在 RequestMapping 注解类中 method() 方法返回的是 RequestMethod 数组,所以可以给 method 同时指定多个请求方式,这里指定fileUpload只能处理POST方法。
deleteFile方法指定接收的url请求路径是http://localhost/SpringMVC/deleteFile,getFiles方法指定可以处理的url请求路径是http://localhost/SpringMVC/deleteFile,两个方法都设定了只能处理GET请求。
@RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public String fileUpload(@RequestParam("file") MultipartFile file) throws JsonProcessingException {}
@RequestMapping(value = "listFiles", method = RequestMethod.GET)
public String getFiles() throws JsonProcessingException {}
分析这个类的初衷是这个类是文件转换的基础,而Spring MVC的框架设计非常巧妙。
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
@Controller
public class FileController {
//传统方法
//@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
//批量上传CommonsMultipartFile则为数组即可
@RequestMapping("upload")
public String upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
//获取文件名字
String uploadFileName = file.getOriginalFilename();
//如果文件名为空,重定向到首页
if("".equals(uploadFileName)){
return "redirect:/index.jsp";
}
System.out.println("上传文件名:"+uploadFileName);
//上传路径保存设置
String path = request.getServletContext().getRealPath("/upload");
//如果路径不存在,创建一个
File realPath = new File(path);
if(!realPath.exists()){
realPath.mkdirs();
}
System.out.println("上传文件保存地址:"+realPath);
InputStream in = file.getInputStream();//文件输入流
/*File:根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。*/
FileOutputStream os = new FileOutputStream(new File(realPath,uploadFileName));//文件输出流
int len=0;
byte[] buff = new byte[1024];
while((len=in.read(buff))!=-1){
os.write(buff,0,len);
os.flush();
}
os.close();
in.close();
return "redirect:/index.jsp";
}
//使用CommonsMultipartFile的transferTo方法
@RequestMapping("upload2")
public String uploadFile(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) throws IOException {
//上传路径保存设置
String path = request.getServletContext().getRealPath("/upload");
//如果路径不存在,创建一个
File realPath = new File(path);
if(!realPath.exists()){
realPath.mkdirs();
}
System.out.println("上传文件保存地址:"+realPath);
//通过CommonsMultipartFile的transferTo方法直接写文件
file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));
return "redirect:/index.jsp";
}
}