~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:java,springMVC文件上传
撰写时间:2020-06-08
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一步:导包(有些是多的,不影响效果)
第二步:全局配置文件
包括有两个配置文件
第一个是(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 配置前端控制器 -->
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第二个是(springmvc.xml当然这个名字随意,只是标准一点)
springMVC的servlet.xml配置文件默认存放在/WEB-INF/文件下的
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.demo.controller"></context:component-scan>
<!-- 注解驱动 -->
<!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->
<!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
<!-- MultipartResovler解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 文件大小上限,当前为100000-->
<property name="maxUploadSize" value="100000"></property>
</bean>
</beans>
Controller类
package com.demo.controller;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class DemoController {
@RequestMapping("upload")
public String upload(MultipartFile file,String name,HttpServletRequest request) throws IOException{
String fileName = file.getOriginalFilename();
// 文件存放地址
File file1 = new File("E:/"+name+fileName);
// 判断存放文件是否有同名的存在
if (file1.exists()) {
System.out.println("文件:" + file1.getPath() + " ··存在,上传失败");
String path=("文件:" + file1.getPath() + " ·· 存在");
request.setAttribute("path", path);
}else {
//截取文件名.后面的字符
//String suffix = fileName.substring(fileName.lastIndexOf("."));
//一串编码
// String uuid = UUID.randomUUID().toString();
// getInputStream():获取文件流,存放位置,后面拼接的是文件名
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:/"+name+fileName));
String path=("文件:" + file1.getPath() + "··不存在,上传成功");
request.setAttribute("path", path);
}
return "/index.jsp";
}
}
HTML代码
<body>
<form action="upload" enctype="multipart/form-data" method="post">
请重新命名姓名:<input id="name" type="text" name="name"/><br/>
文件:<input type="file" name="file"/><br/>
<input id="submit" type="submit" value="提交"/>
保存状态:${path}
</form>
</body>