• springboot上传excel到oss


     
     
    Demo如下:
    package com.ztkj.engine.portal.controller;
    
    import com.aliyun.oss.OSSClient;
    import com.aliyun.oss.model.ObjectMetadata;
    import com.ztkj.engine.common.result.Response;
    import com.ztkj.engine.portal.entity.bean.Region;
    import com.ztkj.engine.portal.service.RegionService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.commons.CommonsMultipartFile;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    /**
     * 版本         开发者               创建日期
     * 1.0.0   InetCommunity(^_^)on     2018/12/25.
     */
    @RestController
    @RequestMapping("/common")
    public class CommonController {
    
        @Value("${OSS.BUCKET_NAME}")
        private String BUCKET_NAME;
        @Value("${OSS.ENDPOINT}")
        private String ENDPOINT;
        @Value("${OSS.ACCESS_KEY_ID}")
        private String ACCESS_KEY_ID;
        @Value("${OSS.ACCESS_KEY_SECRET}")
        private String ACCESS_KEY_SECRET;
        @Value("${OSS.IMG_DOMAIN}")
        private String IMG_DOMAIN;
    
        @Autowired
        private RegionService regionService;
    
        @RequestMapping("/uploadImg")
        public Response uploadImg(@RequestParam(value="file",required = false)CommonsMultipartFile file){
            OSSClient ossClient=new OSSClient(ENDPOINT,ACCESS_KEY_ID,ACCESS_KEY_SECRET);
            String fileType=file.getContentType();
            try{
                ObjectMetadata meta=new ObjectMetadata();
                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=new HashMap();
                map.put("filePath",fileNewName);
                return Response.successResult(map);
            }catch (Exception e){
                return Response.dataError("上传失败,请重试");
                //throw new RuntimeException("上传图片失败,请重试");
            }
            finally{
                if(ossClient!=null){
                    ossClient.shutdown();
                }
            }
        }
    
        /*
        * 生成图片文件全路径
        * */
        public String 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")
        public Response  getRegionByPid(@RequestBody Region region){
            if(region==null||region.getPid()==null){
                //默认显示第一级
                region=new Region();
                region.setPid(0L);
            }
            return this.regionService.getRegionByPid(region);
        }
    }
  • 相关阅读:
    多线程(一) NSThread
    Swift 烧脑体操(一)
    Swift 烧脑体操(二)
    UINavigationController使用的注意事项
    更多请查看我的文章
    本地通知
    网络编程(二)NSURLSessionConfiguration
    A
    51Nod 1116 K进制下的大数(暴力枚举)
    51Nod 1065 最小正子段和
  • 原文地址:https://www.cnblogs.com/duguxiaobiao/p/12091746.html
Copyright © 2020-2023  润新知