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微信开发之接受消息回复图片或者文本
上回说到 接口连接成功,接下来是真正的开发了. 消息的接收,整个过程就是关注订阅号的用户在微信订阅号中发送消息,微信服务器接收到消息,将消息发给开发者的服务器,服务器接收消息然后可以根据内容进行回复. ...
随机推荐
- 福建百度seo和推广,关键词排名优化,网络营销推广培训
福建百度seo和推广,关键词排名优化,网络营销推广培训 福建百度seo和推广,关键词排名优化,网络营销推广培训,那么如何才能够让自己的文章信息被百度收录呢?只要说自己的文章能够被百度收录,那么你的信息 ...
- Python中append和extend的区别
编者注:本文主要参考了<Python核心编程(第二版)> 网上有很多对这两个函数的区别讲解,但我觉得都讲的不是很清楚,记忆不深刻.这样解释清楚且容易记住. list.append(obje ...
- 自兴人工智能-------------Python入门基础(1)
Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构, 并且能够用简单而又高效的方式进行面向对象编程. Python 优雅的语法和动态 类型,再结合它的解释性,使其在大多数平台 ...
- UEditor工具栏上自定义按钮、图标、事件和右击菜单添加自定义按钮
首先我要说是,举例说的这个版本是1.2以上的,因为一些配置代码转移到了zh-cn.js里,其他没有变化.开门见山直接写:(我自定义的是在线美图功能) 第一步:找到ueditor.config.js文件 ...
- Angular Pipe的应用
1-在html文件中使用管道:(管道符合使用,用':'号隔开) ①页面中添加: <div class="table_content" *ngFor="let ite ...
- Mac 上安装 GCC
https://www.zhihu.com/question/20588567 安装 添加bin路径到$PATH变量
- centos出现“FirewallD is not running”怎么办
最近在阿里云服务器centos上安装了mysql数据库,默认是不开启远端访问功能,需要设置一下防火墙,在开放默认端口号 3306时提示FirewallD is not running,经过排查发现是防 ...
- [记]Debian alias 设置, 不设置貌似有点不方便习惯
备忘录,记录下. 不知道 当前有那些 alias 的话 直接输入 alias ,回车就可以看到 alias 列表. 终端输入: vim ~/bash_aliases 然后输入: # some more ...
- 如何使用 VS2015 进行远程调试?
VisualStudio\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger 直接复制 Remote Debugger 文件,里面包含了 ...
- HDU - 1850 Nim博弈
思路:可以对任意一堆牌进行操作,根据Nim博弈定理--所有堆的数量异或值为0就是P态,否则为N态,那么直接对某堆牌操作能让所有牌异或值为0即可,首先求得所有牌堆的异或值,然后枚举每一堆,用已经得到的异 ...