博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc 文件上传
阅读量:6513 次
发布时间:2019-06-24

本文共 3973 字,大约阅读时间需要 13 分钟。

jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="sf" uri="http://www.springframework.org/tags/form"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";%>My JSP 'add.jsp' starting page
file:
file:
file:

 

springmvc.xml配置:

...
...

 

controller:

package com.stone.controller;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Random;import javax.servlet.http.HttpServletRequest;import javax.validation.Valid;import org.apache.commons.io.FileUtils;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.PathVariable;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.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.mvc.support.RedirectAttributes;import org.springframework.web.servlet.view.InternalResourceViewResolver;import com.stone.model.User;@Controller@RequestMapping("/user")@SessionAttributes("loginUser")public class UserController {    private final static Map
users = new HashMap
(); public UserController() { users.put("ldh", new User("ldh", "刘德华", "123", "123@123.com")); users.put("zxy", new User("zxy", "张学友", "123", "123@123.com")); users.put("gfc", new User("gfc", "郭富城", "123", "123@123.com")); users.put("lm", new User("lm", "黎明", "123", "123@123.com")); } @RequestMapping("/users") public String list(Model model) { model.addAttribute("users", users); return "user/list"; } @RequestMapping(value = "/add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute(new User()); return "user/add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@RequestParam(required = false) MultipartFile[] files, HttpServletRequest req) throws IOException { for (MultipartFile file : files) { System.out.println(file.getContentType() + "," + file.getName() + "," + file.getOriginalFilename()); String realpath = req.getSession().getServletContext() .getRealPath("/resources/upload/"); System.out.println(realpath); if (!file.isEmpty()) { FileUtils.copyInputStreamToFile( file.getInputStream(), new File(realpath + File.separator + new Random().nextInt())); } } return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/users"; }
//如果是一个文件的话,使用这样的方式 @RequestMapping(value = "/add", method = RequestMethod.POST)    public String add( MultipartFile files,            HttpServletRequest req) throws IOException {            System.out.println(file.getContentType() + "," + file.getName() + "," + file.getOriginalFilename()); String realpath = req.getSession().getServletContext() .getRealPath("/resources/upload/"); System.out.println(realpath); if (!file.isEmpty()) { FileUtils.copyInputStreamToFile( file.getInputStream(), new File(realpath + File.separator + new Random().nextInt())); } return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/users"; }
}

 

转载地址:http://rzsfo.baihongyu.com/

你可能感兴趣的文章
Jenkins 构建方式有几种
查看>>
程序员修炼之道读后感1
查看>>
work flow 工作流程
查看>>
闭包实例
查看>>
springboot-dokcer
查看>>
Swift-范型
查看>>
(2019)OCP 12c 062考试题库出现大量新题-4
查看>>
Velvet基因组拼接软件的使用方法和参数简介(二)
查看>>
.通过JAVA-Webservices向其它平台提供Hbase服务
查看>>
JHipster生成微服务架构的应用栈(三)- 业务微服务示例
查看>>
python chunk模块
查看>>
ImageView.ScaleType
查看>>
SimpleDateFormat使用详解
查看>>
js防止backspace回退到上一界面(兼容各种浏览器)
查看>>
iOS只给矩形两个边加圆角
查看>>
zoj 3772 Calculate the Function 矩阵相乘+ 线段树查询
查看>>
java获取文件的路径问题
查看>>
Windows7下安装MongoDB(转)
查看>>
SQL Server2005中文版x64安装29506错误解决办法
查看>>
Memcache的客户端连接系列(一) Java
查看>>