其他分享
首页 > 其他分享> > 【学习笔记】SpringMVC实现文件上传

【学习笔记】SpringMVC实现文件上传

作者:互联网

导入jar包

commons-fileupload-1.4.jar和commons-io-2.6.jar

配置springMVC.xml文件

<!-- 文件上传解析器,id="multipartResolver"  -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设置上传文件的大小   (in bytes) -1 indicates no limit (the default)-->
		<property name="maxUploadSize" value="200000"></property>
	</bean>
	
	<!-- 处理上传文件过大发生的异常,跳转到errorupload页面 -->
	<bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	    <property name="exceptionMappings">
	        <props>
	           <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">errorupload</prop>
	        </props>
	    </property>
	
	</bean>

编写controller代码

单文件上传

package com.mvc.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {
	//单文件上传
	@PostMapping("uploadfile.do")
	public String uploadFile(HttpServletRequest request, @RequestParam(value = "id", required = false) int id,
			String name, @RequestParam("myfile") MultipartFile myfile) {
		System.out.println(myfile.getContentType() + "," + myfile.getName() + "," + myfile.getOriginalFilename() + ","
				+ myfile.getSize());
		String path = request.getRealPath("/");
		System.out.println("path" + path);
		System.out.println("name :" + name + ", id:" + id);
		File file = new File(path + myfile.getOriginalFilename());// ? d:/tomcat//xx/xxx.jpg
		try {

			myfile.transferTo(file);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
}

多文件上传

package com.mvc.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {
	//多文件上传
	@PostMapping("uploadfiles.do")
	public String uploadFiles(HttpServletRequest request, @RequestParam(value = "id", required = false) int id,
			String name, @RequestParam("myfile") MultipartFile[] myfiles) {

		for (MultipartFile myfile : myfiles) {
			if (!myfile.getOriginalFilename().isEmpty()) {	
				String path = request.getRealPath("/");
				File file = new File(path + myfile.getOriginalFilename());//
				try {
					// 文件上传操作
					myfile.transferTo(file);
				} catch (IllegalStateException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				}
			}
		}
		return "success";
	}
}

jsp页面实现上传

单文上传

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="uploadfile.do" method="post" enctype="multipart/form-data">
   <input type="text" name="id" value="1"><br/>
   <input type="text" name="name" value="tom"><br/>
   <input type="file" name="myfile"><br/>
    <input type="submit">
 </form>
</body>
</html>

多文件上传

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="uploadfiles.do" method="post" enctype="multipart/form-data">
   <input type="text" name="id" value="1"><br/>
   <input type="text" name="name" value="tom"><br/>
   <input type="file" name="myfile"><br/>
   <input type="file" name="myfile"><br/>
   <input type="file" name="myfile"><br/>
    <input type="submit">
 </form>
</body>
</html>

标签:web,SpringMVC,springframework,myfile,笔记,org,import,上传
来源: https://blog.csdn.net/qq_44940503/article/details/114264308