前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java之spring mvc之文件上传

java之spring mvc之文件上传

作者头像
Vincent-yuan
发布2019-09-11 15:12:02
4220
发布2019-09-11 15:12:02
举报
文章被收录于专栏:Vincent-yuanVincent-yuan

目录结构如下:

注意,下面说的配置文件,一般都是值的src下的配置文件,即mvc.xml。如果是web.xml,则直接说 web.xml

1. 文件上传的注意点

表单必须是post提交,必须将 enctype 设置为 “multipart/form-data”,

使用 commons-fileupload 提交文件,需要添加 commons-fileupload 和 commons-io 的 jar 包。

2.Jsp 页面

代码语言:javascript
复制
<form action="file/upload.do" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/><input type="submit" value="上传"/>
</form>
</body>

3.Controller类

代码语言:javascript
复制
@Controller
//窄化 
@RequestMapping("/file")
public class UploadController {
    @RequestMapping("/upload.do")
    public String upload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws Exception{
        String path=req.getServletContext().getRealPath("/upload");
        //获取文件名
        String fileName=file.getOriginalFilename();
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(new File(path,fileName));
        byte[] buffer = new byte[400];
        int len=0;
        while((len=is.read(buffer))!=-1){
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
        return "redirect:/index.jsp";
    }
}

4. 在配置 文件中添加 multipartResolver

代码语言:javascript
复制
<!-- 文件上传配置 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000"/>
    </bean>

附录:

附一,这里附上mvc.xml的文件内容

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 注解开发适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <!-- 为响应的视图名称加上前缀  -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 为响应的视图名称加上后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 文件上传配置 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000"/>
    </bean>
    <!-- 扫描注解类 -->
    <context:component-scan base-package="cn.sxt.controller"/>
</beans>

这里再附上 WebContent/WEB-INF/ 下的 web.xml 文件内容

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>01springmvc_helloworld</display-name>
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 改变springmvc配置文件的路径及名称 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:mvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档