java SpringWeb 接收安卓android传来的图片集合及其他信息入库存储
公司是做APP的,进公司一年了还是第一次做安卓的接口
安卓是使用OkGo.post("").addFileParams("key",File);
通过这种方式传输图片,就与html前台传是一样的处理方式,直接用 MultipartFile 对象接收就好了。
还有需要注意的,使用接收安卓传来的字段属性,需要使用原始类型(int,String,byte,boolean),不能使用自己定义的对象去接收。
好了,废话不说直接上代码。
package com.ccs.ssmis.app.opinion.controller; import com.ccs.ssmis.app.opinion.entity.OpFeedback;
import com.ccs.ssmis.app.opinion.service.OpFeedbackService;
import com.ccs.ssmis.common.support.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import java.util.List; /**
* 意见反馈信息
* @author jushr
* @date 2018-03-05
*/
@CrossOrigin(value = "*",maxAge = 3600)
@Controller
@RequestMapping("/home/opinion/")
public class OpFeedbackController { @Autowired
private OpFeedbackService opFeedbackService; /**
* 1.添加意见反馈信息
* @param fileList 图片集合
* @param userId 用户编号
* @param content 反馈内容
* @param type 反馈类型
* @param contactWay 联系方式
* @param ext1 备用字段
* @param ext2 备用字段
* @return
* @throws Exception
*/
@RequestMapping(value = "/addOpFeedback", method = RequestMethod.POST)
@ResponseBody
public Result addOpFeedback(List<MultipartFile> fileList,String userId,String content,
String type,String contactWay,String ext1, String ext2) throws Exception {
OpFeedback opFeedback = new OpFeedback(userId,content,type,contactWay,ext1,ext2);
return opFeedbackService.addOpFeedback(opFeedback,fileList);
} /**
* test 测试接口,测试是否能接到图片集合
* @param fileList
* @param userId
* @return
*/
@RequestMapping(value = "/testFiles", method = RequestMethod.POST)
@ResponseBody
public Result testFiles(List<MultipartFile> fileList, String userId) throws Exception { try {
System.out.println("userId=="+userId);
System.out.println(fileList);
for (MultipartFile f:fileList) {
System.out.println("getName=="+f.getContentType());//获取图片类型
System.out.println("getName=="+f.getOriginalFilename());//获取图片名称
// System.out.println("getPath=="+f.getPath());
} return new Result(true,"成功");
}catch (Exception e){
return new Result(false,"失败");
}
} }
下面是关于图片及其他信息的存储及其他处理的代码
package com.ccs.ssmis.app.opinion.service.impl; import com.ccs.ssmis.app.opinion.dao.OpFeedbackMapper;
import com.ccs.ssmis.app.opinion.dao.PictureMapper;
import com.ccs.ssmis.app.opinion.entity.OpFeedback;
import com.ccs.ssmis.app.opinion.entity.Picture;
import com.ccs.ssmis.app.opinion.service.OpFeedbackService;
import com.ccs.ssmis.common.support.db.DataSource;
import com.ccs.ssmis.common.support.dto.Result;
import com.ccs.ssmis.common.utils.Constants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*; @Service
public class OpFeedbackServiceimpl implements OpFeedbackService { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private OpFeedbackMapper opFeedbackMapper; @Autowired
private PictureMapper pictureMapper; @Override
@DataSource("dataSourceORACLE")
public Result addOpFeedback(OpFeedback opFeedback, List<MultipartFile> fileList)throws Exception {
opFeedback.setId("OPFE-"+UUID.randomUUID().toString().toUpperCase());
opFeedback.setIsDelete("1");//默认正常
opFeedback.setCreTime(new Date());//默认当前时间创建
// opFeedback.setCreUserId("");//创建人编号。默认空 待完善
int add = opFeedbackMapper.addOpFeedback(opFeedback);//添加意见反馈信息
if (add > 0){
List<Picture> fuJianList = new ArrayList<Picture>();
int i=0;
try {
if (null!=fileList){
for (MultipartFile file:fileList) {
String fileName =file.getOriginalFilename();//获取图片名称
if (!file.isEmpty()) {
if (file.getSize() > Constants.fujian_size * 1024 * 1024) { //现在附件上传最大为15M
return new Result(false, "图片大小超过15M","图片大小超过15M");
}
String filename = file.getOriginalFilename();
String type = filename.substring(filename.lastIndexOf('.')); if (!StringUtils.isEmpty(Constants.Picture_types) && Constants.Picture_types.indexOf(type) == -1) {
return new Result(false, "图片类型不正确");
} SimpleDateFormat formater = new SimpleDateFormat("yyyy");
String path = "/"+formater.format(new Date())+"/"+opFeedback.getId()+"/";
String rfilename = getName(fileName);//生成文件名
Picture picture = new Picture(opFeedback.getId(),filename,path+"/"+rfilename,type,i);//
picture.setId("OPPI-"+UUID.randomUUID().toString().toUpperCase());//图片id
picture.setCreTime(new Date());//默认创建时间
// picture.setCreUserId("");//创建人编号。默认空 待完善 String realPath=Constants.Picture_path+path;//图片存储绝对路径
//上传图片
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath + getFolder(realPath), rfilename));
add = pictureMapper.addOpinionPicture(picture);
if (add > 0){
fuJianList.add(picture);
}
}
i++;
}
if (fuJianList.size() > 0) {
return new Result(true, fuJianList,"反馈成功");
} else {
return new Result(false, null,"反馈失败");
}
}
}catch (Exception e){
e.printStackTrace();
logger.error("==========意见反馈上传图片时发生异常,addOpFeedback===========================",e);
return new Result(false, null,"反馈异常");
}
} return new Result(true, null,"反馈成功");
} /**
* 依据原始文件名生成新文件名
*
* @return
*/
private String getName(String fileName) {
Random random = new Random();
return "" + random.nextInt(10000) + System.currentTimeMillis() + this.getFileExt(fileName);
} /**
* 获得文件扩展名
*
* @param fileName
* @return
*/
private String getFileExt(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
} private String getFolder(String realPath) { File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdirs();
}
return "";
}
}
本博客是本人原创 转载请注明来处 谢谢。
链接地址:http://www.cnblogs.com/richard-ju/p/L2018001.html
java SpringWeb 接收安卓android传来的图片集合及其他信息入库存储的更多相关文章
- Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片
一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- Android中将一个图片切割成多个图片
有种场景,我们想将一个图片切割成多个图片.比如我们在开发一个拼图的游戏,就首先要对图片进行切割. 以下是封装好的两个类,可以实现图片的切割.仅供参考和学习. 一个是ImagePiece类,此类保存了一 ...
- android Java BASE64编码和解码二:图片的编码和解码
1.准备工作 (1)在项目中集成 Base64 代码,集成方法见第一篇博文:android Java BASE64编码和解码一:基础 (2)添加 ImgHelper 工具类 package com.a ...
- Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...
- 【Android】安卓中常用的图片加载方法
一.通过相机选图片: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- 安卓开发笔记——关于图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)
在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...
- android 73 下载图片
package com.ithiema.imageviewer; import java.io.InputStream; import java.net.HttpURLConnection; impo ...
- java微信开发之接受消息回复图片或者文本
上回说到 接口连接成功,接下来是真正的开发了. 消息的接收,整个过程就是关注订阅号的用户在微信订阅号中发送消息,微信服务器接收到消息,将消息发给开发者的服务器,服务器接收消息然后可以根据内容进行回复. ...
随机推荐
- VUE2.0 elemenui-ui 2.0.X 封装 省市区三级
1. 效果图 2. 版本依赖 vue 2.X , elementui 2.0.11 使用element ui <el-form>标签 3. 源码 components/CityL ...
- SpringMVC之使用requestMapping映射请求、映射参数、映射头
1. 映射请求 作用:使用requestMapping可以指定处理器可以处理那些请求 地方:类和方法前面都可以 @requestMapping 类定义处: 提供初步的请求映射信息,相对于web应用的根 ...
- Spring 中出现Element : property Bean definitions can have zero or more properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, refer
在这个ApplicationContext.xml文件中出现 如下报错 Element : property Bean definitions can have zero or more proper ...
- 使用tdload工具将本地数据导入到Teradata数据库中
想把本地的数据文件(比如txt.csv)中的数据导入到Teradata虚拟机中的表中.既可以使用Teradata Assistant中的import功能,也可以使用fastload导入,前者的缺点是一 ...
- entity framework core在独立类库下执行迁移操作
之前学习EFCore的时候,都是在VS创建的默认模板里面进行的,按照官方文档,直接就可以搞定. 今天新项目准备上.Net Core,打算先按照国际惯例,进行分层,数据访问层是用EFCore来做,于是就 ...
- 剑指offer 丑数
思路:可以发现,每个丑数都是由以前的丑数得到.当前丑数一定是之前丑数能够得到的最小丑数. AC代码 class Solution { public: int GetUglyNumber_Solutio ...
- 【Learning】 动态树分治
简介 动态树分治整体上由点分治发展而来. 点分治是统计树上路径,而动态树分治用来统计与点有关的树上路径,比如多次询问某一些点到询问点的距离和. 前置知识就是点分治. 做法 众所周知,点分树(点分治中重 ...
- 转载微信公众号 测试那点事:Jmeter乱码解决
原文地址: http://mp.weixin.qq.com/s/4Li5z_-rT0HPPQx9Iyi5UQ 中文乱码一直都是比较让人棘手的问题,我们在使用Jmeter的过程中,也会遇到中文乱码问题 ...
- Redis笔记1-redis的搭建和使用
1. Redis的安装 1.1. Redis的安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: ...
- datanode启动不起来的各种原因
一般在数据节点的log日志信息里能找到导致启动不起来的原因. 1.Namenode和Datanode的NamenodeID不一致 描述:一般在集群多次重新格式化HDFS之后,或者刚安装时会碰到.日志信 ...