- 添加依赖
进行文件上传需要添加相关依赖,包括spring-webmvc、spring-context和commons-fileupload。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
- 配置Dispatcher Servlet
在web.xml中配置Dispatcher Servlet,映射URL和应用上下文。
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.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>
- 添加MultipartResolver Bean
MultipartResolver负责处理文件上传请求。创建一个MultipartResolver Bean,并配置其最大上传文件大小等属性。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSizePerFile" value="10485760"/>
</bean>
- 创建Controller
创建一个Spring MVC Controller,在其中添加一个方法来处理文件上传请求。
@Controller
public class FileUploadController {
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(HttpServletRequest request) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multipartRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile multipartFile = multipartRequest.getFile(iterator.next());
if (multipartFile.getSize() > 0) {
String fileName = multipartFile.getOriginalFilename();
String contentType = multipartFile.getContentType();
byte[] bytes = multipartFile.getBytes();
// 处理文件上传,例如保存到本地磁盘
// ...
}
}
return "File upload is successful!";
}
}
- 添加HTML表单
创建一个HTML表单,并指定其enctype属性为multipart/form-data,使用POST请求向/fileUpload URL提交文件上传请求。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Upload Example</title>
</head>
<body>
<form action="/fileUpload" method="POST" enctype="multipart/form-data">
Select a file: <input type="file" name="file" /><br/><br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
示例1:使用Ajax上传文件
可以使用Ajax来提交文件上传请求,示例如下所示:
$(document).ready(function() {
$("#uploadForm").submit(function(event) {
var data = new FormData($(this)[0]);
$.ajax({
url: "/fileUpload",
type: "POST",
data: data,
processData: false,
contentType: false,
success: function(response) {
alert(response);
}
});
event.preventDefault();
});
});
示例2:限制上传文件类型
可以通过MultipartFile.getContentType()方法获取上传文件的类型,可以用getContentType()方法来限制上传文件的类型。例如,只允许上传PDF和图片文件可以使用以下代码:
if (multipartFile.getSize() > 0) {
String fileName = multipartFile.getOriginalFilename();
String contentType = multipartFile.getContentType();
byte[] bytes = multipartFile.getBytes();
// 限制上传文件类型
if (contentType.startsWith("application/pdf") ||
contentType.startsWith("image/")) {
// 处理文件上传,例如保存到本地磁盘
// ...
}
else {
// 文件类型不合法
// ...
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring实现文件上传(示例代码) - Python技术站