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 ...
随机推荐
- android Animation介绍
Animation介绍: 在Android SDK介绍了2种Animation模式: 1. Tween Animation:间动画,通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即 ...
- 利用sphinx为python项目生成API文档
sphinx可以根据python的注释生成可以查找的api文档,简单记录了下步骤 1:安装 pip install -U Sphinx 2:在需要生成文档的.py文件目录下执行sphinx-apido ...
- Mysql Join
在前面的博文中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询 ...
- java.lang.UnsatisfiedLinkError: %1 不是有效的 Win32 应用程序。
JNA 调用 dll 库时,保错: ///////////////// 通过 JNA 引入 DLL 库 //////////// /** * ID_FprCap.dll 负责指纹的采集, 指纹仪的初始 ...
- 安装Appium-windows
安装Appium-windows JDK 安装JDK后设置 环境变量,把环境变量添加到你的系统PATH变量中. 变量: JAVA_HOME 值: C:\Program Files (x86)\Java ...
- JavaScript第一节课
1.用法:位于<script></script>可以位于body和head中,不限制标签数量,也可以创建外部Js文件,然后引入.(引入方法:<script src=&qu ...
- C++ 模版
函数模版 #include <iostream> using namespace std; template<typename T> T add(T t1, T t2) { r ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- kubernetes 1.4.5集群部署
2016/11/16 23:39:58 环境: centos7 [fu@centos server]$ uname -a Linux centos 3.10.0-327.el7.x86_64 #1 S ...
- C-RAN 集中化、协作化、云化、绿色节能(4C)
中国移动C-RAN力拼第4个C:2018年6月外场组网验证 http://www.c114.net ( 2016/11/22 07:41 ) C114讯 11月22日早间消息(子月)2009年,中国移 ...