`

springMVC REST 上传文件

阅读更多

参考:

1.http://www.journaldev.com/2573/spring-mvc-file-upload-example-single-multiple-files

 

上传接口controll:

@RestController
public class OcrController {
	
	private static final Logger logger = LoggerFactory.getLogger(OcrController.class);
	
	@Resource
	private OcrService ocrService;
	
	@RequestMapping(value="/api/ocr",method = RequestMethod.POST)
	public Response getOcrResponse(@RequestParam(value = "file", required = false) MultipartFile file ) {
		logger.info("get OCR services start...");
		
		OcrFailResponse ocrFailResponse = null;
		Response ocrResponse = null;
		
		File serverFile = createFileOnServer(file);
		
		if(serverFile.exists()){
			
			ocrResponse = ocrService.getOcrService(serverFile);
			return ocrResponse;
		}else{
			ocrFailResponse = new OcrFailResponse();
			ocrFailResponse.setCode(Response.RESULT_CODE_ERROR);
			ocrFailResponse.setMessage(OcrFailResponse.RESULT_MESSAGE_FILEUPLOAD_ERROR);
			return ocrFailResponse;
		}
		
	}
	
	
	public File createFileOnServer(MultipartFile file){
		
		File serverFile = null;
		
		if (!file.isEmpty()) {
			try {
				byte[] bytes = file.getBytes();

				// Creating the directory to store file
				String filename = "OcrUplaod_" + TimeUtil.genDate() + ".png";
				File dir = new File(SystemConfig.getFile_store_path());
				if (!dir.exists())
					dir.mkdirs();

				// Create the file on server
				serverFile = new File(dir.getAbsolutePath()
						+ File.separator + filename);
				BufferedOutputStream stream = new BufferedOutputStream(
						new FileOutputStream(serverFile));
				stream.write(bytes);
				stream.close();

				logger.info("Server File Location="
						+ serverFile.getAbsolutePath());
				
				

			} catch (Exception e) {
				logger.error("You failed to upload " + file + " => " + e.getMessage());
			}
		} else {
			logger.debug("You failed to upload " + file + " because the file was empty.");
		}
		
		return serverFile;
	}

}

 

注意要导入commons-fileupload.jar,要不会报错:

the current request is not a multipart request

 

compile 'commons-fileupload:commons-fileupload:1.3.2'

 

添加bean:

<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	</bean>

 

这里可以配置文件上传大小限制:

<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="#{20*1024*1024}"/>
        <property name="resolveLazily" value="true"/>
	</bean>

 

分享到:
评论

相关推荐

    Java源码 SpringMVC Mybatis Shiro Bootstrap Rest Webservice

    集成REST服务,可以用作独立服务平台(提供大量实例及测试平台,包括:文件上传下载、邮件短信发送、部门、产品、公共连接、我的收藏、我的任务、信息发布等) 5. 集成Quartz调度,可以用作定时调度平台(动态...

    springmvc教程

    springmvc详细笔记,文件上传,rest与spring集成

    webuploader分片上传

    既可以上传图片也可以上传文件。文件超过分片大小才启用分片。 3、分片上传已做md5校验,达到秒传的效果。分片以后需要合并,可以先分片后合并,也可以边分片边合并,本示例采用的是边分片边合并的方案。 4、后端...

    SpringMVC的搭建

    SpringMVC搭建的详细步骤,以及控制器、Rest风格的URL、文件上传。

    appstore开发

    Java使用springmvc提供rest api,使用json文件和文件夹进行持久化。包含了app上传 下载 评论 版本控制 等等功能。前台是用ionic,提供移动端和pc端使用界面。

    spring mvc 3.2 参考文档

    DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置(locale)和主题解析(theme resolution),以及文件上传等...

    file-upload-download

    文件上传下载 使用数据库保存,存储,删除和下载文件的简单应用程序。 目录 基本信息 该应用程序允许您在数据库中最多保存200 MB的文件。 Aplication为Rest Api提供了下载文件列表,下载文件内容,将文件保存到...

    基于 SpringBoot ++Dubbo 开发分布式REST服务+源代码+文档说明

    | SpringMVC | MVC框架 | | Spring Boot|构建系统| | Apache Shiro | 安全框架 | | MyBatis | ORM框架 | | MyBatis Generator | 代码生成 | | PageHelper | MyBatis物理分页插件 | | Druid | 数据库连接池 | | ...

    毕业设计之SpringCloud-B2C电子商务平台服务端.zip

    毕业设计之SpringCloud-B2C电子商务平台服务端。 开发技术 Spring,SpringMVC,Spring Cloud相关组件 Mybatis,Maven Mysql,Redis Jquery,Ajax... 利用SpringBoot的文件上传完成用户头像上传,Ajax实现商品多图片上传

    springboot学习思维笔记.xmind

    文件上传配置 自定义HttpMessageConverter MappingJackson2HttpMessageConverter StringHttpMessageConverter 服务器端推送技术 SSE Servlet3.0+异步方法处理 Spring MVC的测试 ...

    spring4.1 restful实现

    现在网上大多数关于restful的资料都是spring3,而spring4进行了一些更改,在此处上传一个基本demo供参考,由于此demo使用的IDE是intellj12,使用其它工具的将配置文件、控制器、包拷入使用即可。

    JavaEE开发的颠覆者SpringBoot实战[完整版].part1

    4.5.1 文件上传配置 98 4.5.2 自定义HttpMessageConverter 101 4.5.3 服务器端推送技术 106 4.6 Spring MVC 的测试 113 4.6.1 点睛 113 4.6.2 示例 114 第三部分 实战Spring Boot 第5 章 Spring Boot 基础 122 5.1 ...

    基于Guns+springboot+dubbo开发+源代码+文档说明

    Guns基于SpringBoot,致力于做更简洁的后台管理系统,完美整合springmvc + shiro + mybatis-plus + beetl!Guns项目代码简洁,注释丰富,上手容易,同时Guns包含许多基础模块(用户管理,角色管理,部门管理,字典管理等10个...

    tg-bot-test-resliv

    该应用程序的源代码必须上传到GitHub,在自述文件中指定启动所需的内容(包括机器人的电报名称及其令牌)。配置将bot包含在application.properties中: app.telegram.bot.enable=trueapp.telegram.b

    Eclipse开发分布式商城系统+完整视频代码及文档

    │ 06.FastDFS安装步骤-文件上传.avi │ 07.配置nginx插件访问图片.avi │ 08.测试图片上传.avi │ 09.FastDFS工具类的使用.avi │ 10.图片上传过程分析.avi │ 11.图片上传Service.avi │ 12.图片上传完成.avi │ ...

Global site tag (gtag.js) - Google Analytics