当前位置:   article > 正文

excel oss 上传_springboot上传excel到oss

excel oss 上传_springboot上传excel到oss

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);

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/599161
推荐阅读
相关标签
  

闽ICP备14008679号