文章目录
SpringMVC获得请求数据
文件上传
客户端表单实现
编写upload.jsp
<%--
Created by IntelliJ IDEA.
User: guigui
Date: 2022/4/21
Time: 18:27
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>
<form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
名称<input type="text" name="username"><br/>
文件<input type="file" name="uploadFile"><br/>
<input type="submit">
</form>
</body>
</html>
成功测试
未编写quick22,所以404
单文件上传的代码实现
导入fileupload和io坐标
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
配置文件上传解析器
编写spring-mvc.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="500000"></property>
</bean>
编写UserController
配置文件上传代码
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick22")
@ResponseBody
public void save22(String username, MultipartFile uploadFile) throws IOException {
//获得文件名
// String originalFilename = uploadFile.getOriginalFilename();
//保存文件
// uploadFile.transferTo(new File("C:\\upload\\" + originalFilename));
//
System.out.println(username);
System.out.println(uploadFile);
}
}
成功测试
编写UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick22")
@ResponseBody
public void save22(String username, MultipartFile uploadFile) throws IOException {
//获得文件名
String originalFilename = uploadFile.getOriginalFilename();
//保存文件
uploadFile.transferTo(new File("D:\\study\\" + originalFilename));
}
}
成功测试
d://Study文件夹下找到taotao
多文件上传实现
修改upload.jsp
<%--
Created by IntelliJ IDEA.
User: guigui
Date: 2022/4/21
Time: 18:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
名称<input type="text" name="username"><br/>
文件1<input type="file" name="uploadFile"><br/>
文件2<input type="file" name="uploadFile2"><br/>
<input type="submit">
</form>
</body>
</html>
修改UserController
成功测试
d://study找到taotao.txt 和liuliu.txt
多文件上传实现(数组形参)
修改upload.jsp
<%--
Created by IntelliJ IDEA.
User: guigui
Date: 2022/4/21
Time: 18:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
名称<input type="text" name="username"><br/>
文件1<input type="file" name="uploadFile"><br/>
文件2<input type="file" name="uploadFile"><br/>
<input type="submit">
</form>
</body>
</html>
修改UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick22")
@ResponseBody
public void save22(String username, MultipartFile[] uploadFiles) throws IOException {
for (MultipartFile uploadFile: uploadFiles
) {
//获得文件名
String originalFilename = uploadFile.getOriginalFilename();
//保存文件
uploadFile.transferTo(new File("D:\\study\\" + originalFilename));
}
}
}