Java笔记之SpringMVC上传文件

0.说在前面

  基于SpringMVC项目

1.导入jar包commons-io-x.x.jar和commons-fileupload-x.x.jar

  我这里使用的是commons-io-2.4.jar和commons-fileupload-1.4.jar

2.新建fileupload.jsp和show.jsp页面

fileupload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
    <form action="fileupload.action" method="post">
        <input type="file" name="myFile"/>
        <button type="submit">上传</button>
    </form>
</body>
</html>

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img alt="图片" src="upload/${fileName }">
</body>
</html>

3.springmvc.xml配置文件上传解析器

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- 开启注解扫描, base-package配置Controller所在的基础包-->
    <context:component-scan base-package="com.springmvc.demo.controller"></context:component-scan>
    
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 配置文件上传解析器,设置最大文件大小为10M(10*1024*1024) -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"></property>
    </bean>
    
</beans>

4.新建FileUploadController类,其中包含初始化跳转到fileupload.jsp的方法(initFileUploadPage)和文件上传的方法(fileUpload)

package com.springmvc.demo.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {
    @RequestMapping("/initFileUploadPage.action")
    public String initFileUploadPage(){
        return "fileupload";
    }
    
    @RequestMapping("/fileupload.action")
    public String fileUpload(HttpServletRequest request,MultipartFile myFile){
        //获取上传文件的原文件名
        String fileName = myFile.getOriginalFilename();
        String basePath = request.getServletContext().getRealPath("/upload");
        File destFile=new File(basePath, fileName);
        try {
            myFile.transferTo(destFile);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        request.setAttribute("fileName", fileName);
        return "show";
    }
}

5.访问http://localhost:8080/springmvc_demo/initFileUploadPage.action,跳转到fileupload.jsp页面,选择了桌面上的一张图片

点击上传按钮进行文件的上传和页面的跳转

6.说明

  (1).这里上传的是图片,自然文件也是不在话下的,主要是注意文件的大小,可以在springmvc.xml中的上传文件解析器中进行文件大小的配置,这里配置的是10M;

  (2).这里将图片上传到了项目部署的环境上的upload文件夹中,这个目的路径根据需要自定义;

  (3).为了避免同一个文件的多次上传互相覆盖或者文件同名问题,可以将文件名进行自定义,使用UUID或者加上微秒/纳秒的时间字符串都可以;

  (4).提交包含文件的表单数据时,form表单上的enctype属性值要设置为multipart/form-data;

  (5).SpringMVC返回视图信息时,可以再ModelAndView中设置视图名称,也可以直接返回一个视图名称字符串,都是可以的,这里就是使用的字符串,将信息放在了request作用域中;

猜你喜欢

转载自www.cnblogs.com/antusheng/p/12653834.html