SpringMVC_05实用技术之文件上传、Restful
文件上传
-
上传页面
<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %> <form action="/fileupload" method="post" enctype="multipart/form-data"> <%--文件上传表单的name属性值一定要与controller处理器中方法的参数对应,否则无法实现文件上传--%> 上传LOGO:<input type="file" name="file"/><br/> 上传照片:<input type="file" name="file1"/><br/> 上传任意文件:<input type="file" name="file2"/><br/> <input type="submit" value="上传"/> </form>
-
MultipartResolver接口定义了文件上传过程中的相关操作,对通用性操作进行了封装
接口底层实现类CommonsMultipartResolver并未自主实现文件上传下载对应的功能,而是调用了apache的文件上传下载组件
<!--文件上传下载--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency>
-
springmvc配置文件
<!--加载文件上传的bean--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置文件上传的最大Byte --> <property name="maxUploadSize" value="1024000000"/> </bean>
-
文件上传Controller
注意事项:
1.文件命名问题,获取上传文件名,并解析文件名与扩展名 file.getOriginalFilename(); 2.文件名过长 3.文件保存路径 ServletContext context = request.getServletContext(); String basePath = context.getRealPath("/uploads"); File file = new File(basePath+"/"); if(!file.exists()) file.mkdirs(); 4.重名问题 String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
@RequestMapping(value = "/fileupload") //参数中定义MultipartFile参数,用于接收页面提交的type=file类型的表单,要求表单名称与参数名相同 public String fileupload(MultipartFile file,MultipartFile file1,MultipartFile file2, HttpServletRequest request) throws IOException { //首先判断是否是空文件,也就是存储空间占用为0的文件 if(!file.isEmpty()){ //如果大小在范围要求内正常处理,否则抛出自定义异常告知用户(未实现) //获取原始上传的文件名,可以作为当前文件的真实名称保存到数据库中备用 String fileName = file.getOriginalFilename(); //设置保存的路径 String realPath = request.getServletContext().getRealPath("/images"); //保存文件的方法,指定保存的位置和文件名即可,通常文件名使用随机生成策略产生,避免文件名冲突问题 file.transferTo(new File(realPath,file.getOriginalFilename())); } //测试一次性上传多个文件 if(!file1.isEmpty()){ String fileName = file1.getOriginalFilename(); //可以根据需要,对不同种类的文件做不同的存储路径的区分,修改对应的保存位置即可 String realPath = request.getServletContext().getRealPath("/images"); file1.transferTo(new File(realPath,file1.getOriginalFilename())); } if(!file2.isEmpty()){ String fileName = file2.getOriginalFilename(); String realPath = request.getServletContext().getRealPath("/images"); file2.transferTo(new File(realPath,file2.getOriginalFilename())); } return "page.jsp"; }
Restful
-
rest(REpresentational State Transfer)
一种网络资源的访问风格,定义了网络资源的访问方式
传统:http://localhost/user/get?id=1
Rest:http://localhost/user/1
-
Restful时按照Rest风格访问网络资源
-
Rest行为约定方式
GET查询 http://localhost/user/1 GET
POST保存 http://localhost/user POST
PUT更新 http://localhost/user PUT
DELETE删除 http://localhost/user DELETE
-
开启SpringMVC对Restful风格的访问支持过滤器,即可以通过页面表单提交PUT与DELETE请求
<filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <servlet-name>DispatcherServlet</servlet-name> </filter-mapping>
-
页面表单使用隐藏域提交请求类型,参数名固定为_method,必须配合提交类型method=post使用
<form action="/user/1" method="post"> <input type="hidden" name="_method" value="PUT"/> <input type="submit"/> </form>
-
类注解@RestController :相当于@Controller和@Responsbody
参数注解@PathVariable:使用路径的变量
@RestController public class UserController { //method属性,定义提交的方式,表示访问的地址是/user/{id},返回的succes.jsp文件须在user路径下 @RequestMapping(value = "/user/{id}",method = RequestMethod.GET) @GetMapping("/user/{id}") public String restGet(@PathVariable String id){ System.out.println("restful is running ....get:"+id); return "success.jsp"; } @RequestMapping(value = "/user/{id}",method = RequestMethod.POST) public String restPost(@PathVariable String id){ System.out.println("restful is running ....post:"+id); return "success.jsp"; } @RequestMapping(value = "/user/{id}",method = RequestMethod.PUT) public String restPut(@PathVariable String id){ System.out.println("restful is running ....put:"+id); return "success.jsp"; } @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE) public String restDelete(@PathVariable String id){ System.out.println("restful is running ....delete:"+id); return "success.jsp"; } }