springMVC文件上传

  springMVC实现文件上传需要将上传文件的表单的method设置为POST,enctype的属性设置为multipart/form-data。只有如此,浏览器才会把用户选择的文件二进制文件数据发送给服务器。

  springMVC文件上传依赖于Apache Commons FileUpload的组件。Apache Commons FileUpload组件共有两个,分别是commons-fileupload和commons-io。下载地址,http://commons.apache.org/proper/。

1、eclipse目录结构

2、上传文件表单

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>upload file test</title>
 8 </head>
 9 <body>
10     <h2>文件上传</h2>
11     <form action="${pageContext.request.contextPath }/upload" enctype="multipart/form-data" method="post">
12         <table>
13             <tr>
14                 <td>文件描述:</td>
15                 <td><input type="text" name="description" /></td>
16             </tr>
17             <tr>
18                 <td>请选择文件:</td>
19                 <td><input type="file" name="file" /></td>
20             </tr>
21             <tr>
22                 <td><input type="submit" value="上传" /></td>
23             </tr>
24         </table>
25     </form>
26 </body>
27 </html>

3、controller

 1 package com.alphajuns.controller;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.multipart.MultipartFile;
12 
13 @Controller
14 public class FileUploadController {
15 
16     @RequestMapping(value="/upload")
17     public String uploadFile(HttpServletRequest request,
18             @RequestParam("description") String description,
19             @RequestParam("file") MultipartFile file) throws Exception {
20         System.out.println(description);
21         // 上传文件不为空,写入上传路径
22         if (!file.isEmpty()) {
23             // 上传文件路径
24             String realPath = request.getServletContext().getRealPath("/images");
25             // 上传文件名
26             String originalFilename = file.getOriginalFilename();
27             File filePath = new File(realPath, originalFilename);
28             // 判断路径是否存在,不存在就创建一个
29             if (!filePath.getParentFile().exists()) {
30                 filePath.getParentFile().mkdirs();
31             }
32             // 将文件保存到一个目标文件中
33             file.transferTo(new File(realPath + File.separator + originalFilename));
34             System.out.println("文件上传路径:" + (realPath + File.separator + originalFilename));
35             return "success";
36         } else {
37             return "error";
38         }
39     }
40     
41 }

4、springMVC配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xmlns:context="http://www.springframework.org/schema/context"
 5         xmlns:mvc="http://www.springframework.org/schema/mvc"
 6         xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12     
13     <!-- 配置@Controller注解扫描 -->
14     <context:component-scan base-package="com.alphajuns.controller"></context:component-scan>
15     <!-- 配置处理器映射器 -->
16     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
17     <!-- 配置处理器适配器 -->
18     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
19     <!-- 配置视图解析器 -->
20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
21         <property name="prefix" value="/WEB-INF/jsp/"></property>
22         <property name="suffix" value=".jsp"></property>
23     </bean>
24     
25     <!-- 文件上传配置 -->
26     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
27         <!-- 文件上传大小限制,单位为字节 -->
28         <property name="maxUploadSize">
29             <value>10485760</value>
30         </property>
31         <!-- 请求的编码格式,必须和jsp页面的pageEncoding一致,以便正确读取表单内容,默认为ISO-8859-1 -->
32         <property name="defaultEncoding">
33             <value>UTF-8</value>
34         </property>
35     </bean>
36 </beans>

对比之前自己做过的class1013.top使用kindeditor上传图片,当时用的kindeditor的jsp实现文件上传图片,好像需要自己手动在发布的目录下建立一个存放上传图片的文件夹,这一点有待继续考证。

posted @ 2019-06-19 00:14  AlphaJunS  阅读(323)  评论(0编辑  收藏  举报