ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-03-01 15:58:07  阅读:178  来源: 互联网

标签:web SpringMVC springframework myfile 笔记 org import 上传


导入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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有