spring mvc 配置文件

<bean class="com.baiyyy.yfz.core.RestfulHandlerMethodMapping" />
        <bean id="multipartResolver"  
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <property name="defaultEncoding" value="utf-8" />  
            <property name="maxUploadSize" value="10485760000" />  
            <property name="maxInMemorySize" value="40960" />  
        </bean>

package com.baiyyy.yfz.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile; import com.baiyyy.yfz.core.BaseController;
import com.baiyyy.yfz.util.DateUtil;
import com.baiyyy.yfz.util.PictureUploadPath; /**
* 基础服务接口
*
* @author 左立军
*
*/
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController { /**
* 图片路径配置
*/
@Autowired
private PictureUploadPath pictureUploadPath; @RequestMapping(value = "/picture", consumes = "multipart/form-data", method = RequestMethod.POST)
public void picture(@RequestParam("fileUpload") CommonsMultipartFile file) { // 判断文件是否存在
if (!file.isEmpty()) {
String path = pictureUploadPath.uploadPicturePath + "/"
+ DateUtil.convertDateToYYYYMMdd(new Date()) + "/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
} path += file.getOriginalFilename();
File localFile = new File(path);
try {
file.transferTo(localFile);
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} 多文件上传
@RequestMapping(value = "/picture", consumes = "multipart/form-data", method = RequestMethod.POST)
public void picture(HttpServletRequest request) { String uploadPicturePath = pictureUploadPath.getUploadPicturePath();
String pathname = uploadPicturePath + "/"
+ DateUtil.convertDateToYYYYMMdd(new Date()) + "/";
File file = new File(pathname);
if (!file.exists()) {
file.mkdirs();
}
MultipartHttpServletRequest muti = (MultipartHttpServletRequest) request;
System.out.println(muti.getMultiFileMap().size()); MultiValueMap<String, MultipartFile> map = muti.getMultiFileMap();
for (Map.Entry<String, List<MultipartFile>> entry : map.entrySet()) { List<MultipartFile> list = entry.getValue();
for (MultipartFile multipartFile : list) {
try {
multipartFile.transferTo(new File(pathname
+ multipartFile.getOriginalFilename()));
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}
}
}

 用到的包
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

java spring mvc restful 上传文件的更多相关文章

  1. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  2. spring mvc CommonsMultipartResolver上传文件异常处理

    近期已经上线的项目出现了一个异常 严重: Servlet.service() for servlet JeeCmsAdmin threw exception org.apache.commons.fi ...

  3. spring mvc MultipartFile 上传文件 当文件较小时(10k) ,无法上传成功 。

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" cla ...

  4. spring mvc + ajax上传文件,页面局部刷新

    1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取, 因为包含文件的上传,所以采用FormData的形式来进行数据交互,通过append将 ...

  5. spring mvc MultipartFile 上传文件错误解决

    Field error in object 'xxxx' on field 'xxxx': rejected value [20129259128131.jpg]; codes [typeMismat ...

  6. Spring MVC实现上传文件报错解决方案

    报错代码: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.sp ...

  7. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  8. java的几种上传文件方法

    这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartreso ...

  9. Asp.Net Mvc异步上传文件的方式

    今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...

随机推荐

  1. 转载一篇React native的props的用法

    注:默认值如何设置 http://www.tuicool.com/articles/uMfYv2q

  2. 看完你也能独立负责项目!产品经理做APP从头到尾的所有工作流程详解!

    (一)项目启动前 从事产品的工作一年多,但自己一直苦于这样或者那样的困惑,很多人想要从事产品,或者老板自己创业要亲自承担产品一职,但他们对产品这个岗位的认识却不明晰,有的以为是纯粹的画原型,有的是以为 ...

  3. 【转】深入浅出JavaScript之this

    JavaScript中的this比较灵活,根据在不同环境下,或者同一个函数在不同方式调用下,this都有可能是不同的.但是有一个总的原则,那就是this指的是,调用函数的那个对象. 下面是我的学习笔记 ...

  4. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  5. mysql中的多行查询结果合并成一个

    SELECT GROUP_CONCAT(md.data1) FROM DATA md,contacts cc WHERE md.conskey=cc.id AND md.mimetype_id= 5 ...

  6. Python的模块引用和查找路径

    模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译型的语言,比如C#中 ...

  7. 搭建基于PHP的www服务器

    安装MySQL #!/bin/bash mount |grep "/dev/sr0" if [ "$?" != 0 ];then mount /dev/sr0 ...

  8. [No0000AB]用Visual Studio 2015在 WIN10 64bit 上编译7-zip (32 bit)

    1.7-ZIP简介 7-zip 是一款免费的压缩解压软件.ZIP格式的文件默认被苹果和微软支持,完全不需要额外安装其他软件就可以解压.但对于非US-ASCII编码的文件名和大于2GB的ZIP文件,可能 ...

  9. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  10. valueOf和toString的区别

    基本上所有的JavaScript数据类型都有valueOf(),toString()方法,null除外,这两个方法解决了JavaScript值运算和显示的问题 valueOf()会把数据类型转换成原始 ...