赞
踩
packagecom.ztkj.engine.portal.controller;importcom.aliyun.oss.OSSClient;importcom.aliyun.oss.model.ObjectMetadata;importcom.ztkj.engine.common.result.Response;importcom.ztkj.engine.portal.entity.bean.Region;importcom.ztkj.engine.portal.service.RegionService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.commons.CommonsMultipartFile;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.HashMap;importjava.util.Map;importjava.util.UUID;/*** 版本 开发者 创建日期
* 1.0.0 InetCommunity(^_^)on 2018/12/25.*/@RestController
@RequestMapping("/common")public classCommonController {
@Value("${OSS.BUCKET_NAME}")privateString BUCKET_NAME;
@Value("${OSS.ENDPOINT}")privateString ENDPOINT;
@Value("${OSS.ACCESS_KEY_ID}")privateString ACCESS_KEY_ID;
@Value("${OSS.ACCESS_KEY_SECRET}")privateString ACCESS_KEY_SECRET;
@Value("${OSS.IMG_DOMAIN}")privateString IMG_DOMAIN;
@AutowiredprivateRegionService regionService;
@RequestMapping("/uploadImg")public Response uploadImg(@RequestParam(value="file",required = false)CommonsMultipartFile file){
OSSClient ossClient=newOSSClient(ENDPOINT,ACCESS_KEY_ID,ACCESS_KEY_SECRET);
String fileType=file.getContentType();try{
ObjectMetadata meta=newObjectMetadata();
meta.setContentLength(file.getSize());
String fileName=file.getOriginalFilename();
String fileNewName=fileNameGenerator()+fileName.substring(fileName.lastIndexOf("."));
ossClient.putObject(BUCKET_NAME,fileNewName,file.getInputStream(),meta);
Map map=newHashMap();
map.put("filePath",fileNewName);returnResponse.successResult(map);
}catch(Exception e){return Response.dataError("上传失败,请重试");//throw new RuntimeException("上传图片失败,请重试");
}finally{if(ossClient!=null){
ossClient.shutdown();
}
}
}/** 生成图片文件全路径
**/
publicString fileNameGenerator(){
Calendar date=Calendar.getInstance();
SimpleDateFormat formatYear=new SimpleDateFormat("yyyy");
SimpleDateFormat formatMonth=new SimpleDateFormat("MM");
SimpleDateFormat formatDay=new SimpleDateFormat("dd");
String year=formatYear.format(date.getTime());
String month=formatMonth.format(date.getTime());
String day=formatDay.format(date.getTime());
String fileName=UUID.randomUUID().toString();return year+"/"+month+"/"+day+"/"+fileName;
}
@RequestMapping(value="/getRegionByPid")publicResponse getRegionByPid(@RequestBody Region region){if(region==null||region.getPid()==null){//默认显示第一级
region=newRegion();
region.setPid(0L);
}return this.regionService.getRegionByPid(region);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。