| | |
| | | import com.core.common.SnowflakeIdWorker; |
| | | import com.core.exception.CoolException; |
| | | import lombok.Data; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.security.SecureRandom; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 阿里云OSS服务类 |
| | | * Created by vincent on 2019-11-26 |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | | @Service("ossService") |
| | | public class OssService { |
| | | |
| | | private static Logger log = LoggerFactory.getLogger(OssService.class); |
| | | |
| | | private static final String OSS_ENDPOINT = "http://oss-cn-hangzhou.aliyuncs.com"; |
| | | |
| | |
| | | * 上传OSS文件 |
| | | * @return the url |
| | | */ |
| | | public String upload(InputStream inputStream, String suffix){ |
| | | if(suffix.trim().equals("file")){ |
| | | suffix="jpg"; |
| | | } |
| | | public String upload(InputStream inputStream, String fileName){ |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | String name = ""; |
| | | try { |
| | | if (!ossClient.doesBucketExist(bucket)) { |
| | | ossClient.createBucket(bucket); |
| | | log.info("您的Bucket不存在,创建Bucket:{}",bucket); |
| | | } |
| | | name = String.valueOf(snowflakeIdWorker.nextId()).concat(".").concat(suffix); |
| | | // 添加 ContentType |
| | | String suffix = Objects.requireNonNull(fileName).substring(fileName.lastIndexOf(".") + 1); |
| | | String name = String.valueOf(snowflakeIdWorker.nextId()).concat(".").concat(suffix); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType("image/jpg"); |
| | | ossClient.putObject(bucket, name, inputStream, objectMetadata); |
| | | Date expiration = new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 100); |
| | | URL url = ossClient.generatePresignedUrl(bucket, name, expiration); |
| | |
| | | // return String.valueOf(url); |
| | | return prefix+name; |
| | | } catch (Exception e){ |
| | | log.error("OSS文件上传失败!文件名{}", fileName); |
| | | e.printStackTrace(); |
| | | } finally { |
| | | ossClient.shutdown(); |
| | | } |
| | | throw new CoolException("500-上传失败"); |
| | | } |
| | | |
| | | public String upload(InputStream inputStream) { |
| | | return upload(inputStream, ".jpg"); |
| | | } |
| | | |
| | | /** |
| | |
| | | ossClient.shutdown(); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | File file = new File("E:\\tmp\\v2-fbbb97b977b5cebc66dc3cefab0ac981_r.jpg"); |
| | | try { |
| | | InputStream in = new FileInputStream(file); |
| | | OssService ossService = new OssService(); |
| | | ossService.setAccessKeyId("LTAI4GDzr6ioSHuRw2mk22ug"); |
| | | ossService.setAccessKeySecret("84CHL7tF21LbU1qpaP0jn9mIAZP9bv"); |
| | | ossService.setBucket("tjdt"); |
| | | ossService.setEndpoint("http://oss-cn-hangzhou.aliyuncs.com"); |
| | | ossService.setSnowflakeIdWorker(new SnowflakeIdWorker()); |
| | | System.out.println(ossService.upload(in)); |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public String uploadFile(InputStream inputStream, String fileName, Long len, String suffix){ |