java spring mvc restful 上传文件
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 上传文件的更多相关文章
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- spring mvc CommonsMultipartResolver上传文件异常处理
近期已经上线的项目出现了一个异常 严重: Servlet.service() for servlet JeeCmsAdmin threw exception org.apache.commons.fi ...
- spring mvc MultipartFile 上传文件 当文件较小时(10k) ,无法上传成功 。
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" cla ...
- spring mvc + ajax上传文件,页面局部刷新
1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取, 因为包含文件的上传,所以采用FormData的形式来进行数据交互,通过append将 ...
- spring mvc MultipartFile 上传文件错误解决
Field error in object 'xxxx' on field 'xxxx': rejected value [20129259128131.jpg]; codes [typeMismat ...
- Spring MVC实现上传文件报错解决方案
报错代码: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.sp ...
- spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置
spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...
- java的几种上传文件方法
这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartreso ...
- Asp.Net Mvc异步上传文件的方式
今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...
随机推荐
- iOS字体加载三种方式
静态加载 动态加载 动态下载苹果提供的多种字体 其他 打印出当前所有可用的字体 检查某字体是否已经下载 这是一篇很简短的文章,介绍了 iOS 自定义字体加载的三种方式. 静态加载 这个可以说是最简单最 ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- django 一些相关问题
这两天在处理django项目时碰到一些问题 1.ur路径设置要忽略大小写,查找了很多资料,都没有发现相关的介绍,最后在谷歌上找到一个解决方案,https://groups.google.com/for ...
- bootstrap-datetimepicker在火狐下报错的问题
问题:使用bootstrap-datetimepicker这个日期插件来显示日期,但在火狐下报如下错误:TypeError: (intermediate value).toString(...).sp ...
- ActiveMQ笔记(7):如何清理无效的延时消息?
ActiveMQ的延时消息是一个让人又爱又恨的功能,具体使用可参考上篇ActiveMQ笔记(6):消息延时投递,在很多需要消息延时投递的业务场景十分有用,但是也有一个缺陷,在一些大访问量的场景,如果瞬 ...
- css一些进入条
http://www.jq22.com/jquery-info5309 http://www.jq22.com/jquery-info10964 http://www.jq22.com/jquery- ...
- KPI:Key Performance Indicator
通信中KPI,是Key Performance Indicators的缩写,意思是关键性能指标.performance 还有绩效:业绩的意思,但显然不适用于这种场合. 通信中KPI的内容有:掉话率.接 ...
- 修改Linux用户的UID、GID
对于NFS共享文件,保留文件权限,需要UID.GID与nfs-server端一致! 试验环境:Centos6.5_64/172.24.0.26 01.用户的UID和GID不能被占用 [root@26 ...
- CentOS配置git和maven自动部署java
#安装Git yum install git #测试是否成功 git -version #正确 #git version 1.7.1 #配置git config --global user.name ...
- 关于若干php 表单 的重新审视
对于表单验证来说 应该在任何可能的时候对用户输入进行验证(通过客户端脚本).浏览器验证速度更快,并且可以减轻服务器的负载. 如果用户输入需要插入数据库,您应该考虑使用服务器验证.在服务器验证表单的一种 ...