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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值