SpringMVC_进阶(文件的上传)
2023-11-29 10:47:35 星期三
依赖引入
<dependencies>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--spring基础依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!--spring web依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.20</version>
</dependency>
<!--spring mvc 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
spring-mvc.xml核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.method.annotation.RequestMappingHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--处理器-->
<context:component-scan base-package="com.bboy.controller"/>
<!--
文件上传的加载器:
注意:id的内容必须是 multipartResolver
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--文件上传的最大值-->
<property name="maxUploadSize" value="10485760"/>
</bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
MyFileController
package com.bboy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/29 0029 10:55:21
*/
@Controller
public class MyFileController {
/**
* fileUpload(MultipartFile myfile, HttpServletRequest request)
* 参数-1:MultipartFile myfile
* 指代上传的文件,参数名必须与页面中 file组件的name值一致
* (注意:此处的请求地址不要使用/upload,因为后面有/upload的其它配置)
*/
@RequestMapping("/fileUpload")
public String fileUpload(MultipartFile myfile, HttpServletRequest request, Model model)throws Exception{
//1.获取本地服务目录
String path = request.getSession().getServletContext().getRealPath("upload");
//2. 判断该目录是否存在
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
//3. 将上传的文件保存到本地服务
//3.1 获取文件原始名称
String old_name = myfile.getOriginalFilename();
//3.2 生成新的名称:随机生成
String new_name = UUID.randomUUID().toString().replace("-","");
//3.3 新的名称 = 随机生成的名字 + 文件的后缀名
String hzm = old_name.substring(old_name.lastIndexOf(".")+1);
new_name = new_name +"." + hzm;
//3.4 定义上传的文件
File target = new File(path+"/"+new_name);
//3.5 直接上传
myfile.transferTo(target);
//4.获取网络访问的地址
//4.1 获取基本路径 http://locahost:8080/xxx/upload
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
String imgPath = "/upload/"+new_name;
String imgHttpPath = basePath+imgPath;
System.out.println(imgHttpPath);
//-5.将图片的显示路径返回给页面
model.addAttribute("imgHttpPath",imgHttpPath);
return "/WEB-INF/show.jsp";
}
}
index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<form action="/fileUpload" method="post" enctype="multipart/form-data" >
<input type="file" name="myfile"/><br/>
<input type="submit" value="上传"/>
</form>
</html>
show.jsp
<%--
Created by IntelliJ IDEA.
User: new
Date: 2023/11/28
Time: 10:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="${imgHttpPath}"/>
</body>
</html>