公司是做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