首页>代码>Spring MVC实现的带参数的form表单多文件上传>/WjwTest/src/com/jadyer/controller/UserController.java
package com.jadyer.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.jadyer.model.User;

@Controller
@RequestMapping("/user")
public class UserController {
	private final static Map<String, User> users = new HashMap<String, User>();

	// 模拟数据源,构造初始数据
	public UserController() {
		users.put("张起灵", new User("张起灵", "闷油瓶", "02200059",
				"menyouping@yeah.net"));
		users.put("李寻欢", new User("李寻欢", "李探花", "08866659",
				"lixunhuan@gulong.cn"));
		users.put("拓拔野", new User("拓拔野", "搜神记", "05577759",
				"tuobaye@manhuang.cc"));
		users.put("孙悟空", new User("孙悟空", "美猴王", "03311159",
				"sunhouzi@xiyouji.zh"));
	}

	@RequestMapping("/list")
	public String list(Model model) {
		model.addAttribute("users", users);
		return "user/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addUser() {
		return "user/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addUser(User user, @RequestParam MultipartFile[] myfiles,
			HttpServletRequest request) throws IOException {
		// 如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
		// 如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
		// 并且上传多个文件时,前台表单中的所有<input
		// type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
		for (MultipartFile myfile : myfiles) {
			// if (myfile.isEmpty()) {
			// System.out.println("文件未上传");
			// } else {
			System.out.println("文件长度: " + myfile.getSize());
			System.out.println("文件类型: " + myfile.getContentType());
			System.out.println("文件名称: " + myfile.getName());
			System.out.println("文件原名: " + myfile.getOriginalFilename());
			System.out.println("========================================");
			// 如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
			// 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
			// String realPath =
			// request.getSession().getServletContext().getRealPath("/FileTest/WebContent/WEB-INF/upload");
			String realPath = request.getSession().getServletContext()
					.getRealPath("/WEB-INF/upload");
			System.out.println(realPath);

			FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(
					realPath, myfile.getOriginalFilename()));
			// }
		}
		users.put(user.getUsername(), user);
		return "redirect:/user/list";
	}
}
最近下载更多
hongyan317  LV11 2020年9月20日
gy964781920  LV5 2020年5月16日
solider12  LV8 2020年3月15日
13940562934  LV22 2019年11月13日
yaoxingxing1  LV13 2019年5月17日
zhh1355  LV14 2019年5月10日
hwqhwq  LV20 2019年4月3日
zlongj2015  LV3 2018年11月20日
大鹏小镇  LV14 2018年7月14日
黄丫头  LV10 2018年7月2日
最近浏览更多
1358849392  LV21 4月12日
zhuohanyuan  LV10 2022年9月25日
yymmdm  LV6 2022年9月5日
小翊杭宝  LV2 2022年6月21日
czr2233  LV9 2021年9月14日
1585596474 2021年8月28日
暂无贡献等级
不喝冰阔落 2021年7月18日
暂无贡献等级
cxcxcxcxcx123132 2021年4月2日
暂无贡献等级
zlstly  LV7 2021年3月15日
peterliu  LV3 2020年12月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友