主要介绍三种文件上传的方式:
1、传统文件上传
a、form表单的enctype必须写成enctype="multipart/form-data"
b、表达提交的方式必须是method="post"
c、提供
d、提供commons-fileupload-1.3.1.jar和commons-io-2.4.jar(pom.xml中配置)
2、SpringMVC文件上传
a、需要在springmvc.xml中配置文件解析器CommonsMultipartResolver,且id名必须写成multipartResolver
b、jsp中写的上传文件名必须和controller的方法的接收参数名称相同
3、跨服务器文件上传
a、建立新项目做为文件服务器,且项目中webapp路径下新建文件上传路径uploads,且需要手动在target目录下建立uploads文件夹,
部署到tomcat中(如果是同一台机器测试,需改两个端口 ),并启动
b、pom.xml中引入跨服务上传文件所需的连个jar包jersey-core和jersey-client
c、编写controller层的上传文件相关的代码
首先:定义跨服务器上传文件的路径
其次:利用springMVC的方法获取文件名称
然后:建立客户端对象
然后:和文件服务器建立连接
最后:上传文件
fileUploadTest.jsp页面如下:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/8
Time: 10:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
测试文件上传
传统文件上传
SpringMVC文件上传
跨服务器文件上传
pom.xml配置内容如下:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.example
springmvc_day01_01_start
1.0-SNAPSHOT
war
springmvc_day01_01_start Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
5.0.2.RELEASE
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework.boot
spring-boot-devtools
com.fasterxml.jackson.core
jackson-databind
2.9.0
com.fasterxml.jackson.core
jackson-core
2.9.0
com.fasterxml.jackson.core
jackson-annotations
2.9.0
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
com.sun.jersey
jersey-core
1.18.1
com.sun.jersey
jersey-client
1.18.1
springmvc_day01_01_start
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
springmvc.xml配置如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
FileuploadController.java配置如下:
package com.example.controller;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
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.List;
import java.util.UUID;
@Controller
@RequestMapping("fileupload")
public class FileuploadController {
public static final String SUCCESS = "success";
@RequestMapping("normalUpload")
public String normalUpload(HttpServletRequest request)throws Exception{
System.out.println("执行了传统形式的文件上传...");
//获取上传文件路径
String path = request.getSession().getServletContext().getRealPath("/uploads/");
//判断路径是否存在,若不存在,则创建路径
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
//创建磁盘文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//根据工厂创建ServletFileUpload
ServletFileUpload upload = new ServletFileUpload(factory);
//解析request,得到文件项集合,可以理解为表单中的一项项内容
ListfileItems = upload.parseRequest(request);
for(FileItem item:fileItems){
//遍历文件项
if(item.isFormField()){
//普通的表单项
}else{
//不是表单项,说明是文件项
//获取文件名称
String fileName = item.getName();
//防止文件名相同覆盖已上传的文件,拼接uuid
String uuid = UUID.randomUUID().toString().replace("-","");
fileName = uuid + "_" + fileName;
//完成文件上传
item.write(new File(path,fileName));
//删除临时文件
}
}
return SUCCESS;
}
@RequestMapping("springMVCUpload")
/*此处的参数名称必须和页面传递过来的文件名称相同*/
public String springMVCUpload(HttpServletRequest request,MultipartFile springMvcFile)throws Exception{
System.out.println("执行了SpringMVC形式的文件上传...");
//获取上传文件路径
String path = request.getSession().getServletContext().getRealPath("/uploads/");
//判断路径是否存在,若不存在,则创建路径
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
//配置的文件解析器的作用就是相当于传统的文件上传步骤中我们手动解析request的过程
//所以我们不再需要解析的步骤,方法参数得到的springMVCFile就相当于我们获取到了文件表单项
//获取文件名称
String fileName = springMvcFile.getOriginalFilename();
//防止文件名相同覆盖已上传的文件,拼接uuid
String uuid = UUID.randomUUID().toString().replace("-","");
fileName = uuid + "_" + fileName;
//文件上传,且不许要手动删除临时文件
springMvcFile.transferTo(new File(path,fileName));
return SUCCESS;
}
@RequestMapping("overServerUpload")
public String overServerUpload(MultipartFile springMvcFile2)throws Exception{
//由于是跨服务器上传文件,所以不再需要request获取本服务器的上传路径了
//定义上传服务器文件路径
String path = "http://localhost:9090/fileuploadserver/uploads/";
System.out.println("执行了跨服务器文件上传...");
//配置的文件解析器的作用就是相当于传统的文件上传步骤中我们手动解析request的过程
//所以我们不再需要解析的步骤,方法参数得到的springMVCFile就相当于我们获取到了文件表单项
//获取文件名称
String fileName = springMvcFile2.getOriginalFilename();
//防止文件名相同覆盖已上传的文件,拼接uuid
String uuid = UUID.randomUUID().toString().replace("-","");
fileName = uuid + "_" + fileName;
//跨服务器上传文件,需先创建客户端对象
Client client = Client.create();
//和图片服务器建立连接
WebResource resource = client.resource(path + fileName);
//上传文件
resource.put(springMvcFile2.getBytes());
return SUCCESS;
}
}
SpringMVC上传文件的思路图如下:
若有理解不到之处,望指正!