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);
    }
}
原文地址:https://www.cnblogs.com/duguxiaobiao/p/12091746.html