1.页面端
<form action="mem/upload" method="post" enctype="multipart/form-data" onsubmit="return checkForm(this);"> <input type="hidden" name="flag" value="3"/><input type="hidden" name="memNo" value="${mem.memNo}"/><input type="hidden" name="authSt" value="${mem.idCardAuth}"/> <c:if test="${mem.idCardAuth==0}"><input type="file" name="authFile"/><input type="submit" value="上传"/></c:if> <c:if test="${mem.idCardAuth!=0}"><input type="file" name="authFile"/><input type="submit" value="重新上传"/></c:if> </form>
2.控制器
@RequestMapping(value="upload",method=RequestMethod.POST) public String upload(DefaultMultipartHttpServletRequest multipartRequest,@RequestParam int memNo, @RequestParam int flag,@RequestParam int authSt, HttpServletResponse response){ if (multipartRequest != null) { memberService.uploadMemPic(multipartRequest,memNo, flag,authSt); } //uploadService.json_encode(response, list); //return "redirect:/pic/upload/"+carNo; return "redirect:/mem/detail/"+memNo; }
3.Service层方法
/** * * @param multipartRequest * @param memNo * @param picKey * @param flag 1::本人照片,3:身份证正面照,4:身份证背面,5:驾照照片 * @param authSt 0:未上传 */ public void uploadMemPic(DefaultMultipartHttpServletRequest multipartRequest, int memNo, int flag, int authSt) { Map<String,Object> map = new HashMap<String,Object>(); map.put("memNo", memNo); map.put("flag", flag); Map<String,Object> memPath = memberMapper.getMemBasePath(map); String basePath = (String)memPath.get("basePath"); String oldPicKey = (String)memPath.get("picKey"); if(authSt != 0 && StringUtils.hasText(oldPicKey)){ OSSUtils.deleteOSSObjectAuth(oldPicKey);//删除之前的图片 } MultipartFile multifile = multipartRequest.getFile("authFile"); String picName = multifile.getOriginalFilename(); String extName = picName.substring(picName.lastIndexOf(".")); String newPicName = FILE_NAME_MAPING.get(flag) + extName; String picKey = "mem/" + basePath + newPicName; map.put("picKey", picKey); memberMapper.updateAuth(map);//更新数据库表 try { // OSSUtils.uploadInputStreamAuth(picKey, multifile); //压缩身份证和驾照图片, 140918 InputStream input = null; input = multifile.getInputStream(); //Image image = ImageIO.read(input); BufferedImage bufImg = ImageIO.read(input);//把图片读入到内存中
//压缩代码
bufImg = Thumbnails.of(bufImg).size(bufImg.getWidth(), bufImg.getHeight()).outputQuality(0.9).asBufferedImage();
OSSUtils.uploadInputStreamAuth(picKey, bufImg); } catch (Exception e) { logger.error("",e); } }
4.OSSUtils工具类
public static boolean uploadInputStreamAuth(String key, BufferedImage image) throws Exception{ boolean errorCode = false; ObjectMetadata objectMeta = new ObjectMetadata(); objectMeta.setContentType("image/*");//在metadata中标记文件类型 // InputStream input = multifile.getInputStream(); // InputStream inputTmp = multifile.getInputStream(); //-------------------- //转成inputStream ByteArrayOutputStream bos = new ByteArrayOutputStream();//存储图片文件byte数组 ImageOutputStream ios = ImageIO.createImageOutputStream(bos); ImageIO.write(image, "jpg", ios); //图片写入到 ImageOutputStream InputStream input = new ByteArrayInputStream(bos.toByteArray()); InputStream inputTmp = new ByteArrayInputStream(bos.toByteArray()); //-------------------- try { objectMeta.setContentLength(input.available()); //校验文件MD5值 PutObjectResult res = client.putObject(BUCKET_AUTH, key, input, objectMeta); String checkMD5 = res.getETag(); String verifyCode = getFileInputStreamMD5(inputTmp); if(verifyCode.equalsIgnoreCase(checkMD5) == true){ logger.info("文件上传成功:{}",key); errorCode = true; }else{ logger.error("文件上传失败:{}",key); } } catch (Exception e) { logger.error("",e); }finally{ if(inputTmp != null){ inputTmp.close(); } } return errorCode; }
时间: 2024-10-08 22:21:59