zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    BZOJ 1088 模拟(扫雷经验…)
    BZOJ 1529
    BZOJ 3224
    BZOJ 1192
    BZOJ 1012
    博客搬家说明
    BZOJ 2423 DP
    BZOJ 1789&1830 推式子 乱搞
    BZOJ 1588
    拆点:虫洞
  • 原文地址:https://www.cnblogs.com/richard-ju/p/L2018001.html
Copyright © 2011-2022 走看看