美文网首页
spring boot 文件上传到阿里云OSS

spring boot 文件上传到阿里云OSS

作者: 隔壁丨王大爷 | 来源:发表于2018-05-18 15:29 被阅读142次

    oss配置类,在application.yml文件中加入
    oss:
        bucketName:
        endpoint:
        accessKeyId:
        accessKeySecret:

    package com.example.upload.vo;
    import lombok.Getter;
    import lombok.Setter;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    @Component
    @ConfigurationProperties(prefix = "oss")
    public class OSSConfig {
    
        @Getter @Setter
        private String bucketName;
        @Getter @Setter
        private String endpoint;
        @Getter @Setter
        private String accessKeyId;
        @Getter @Setter
        private String accessKeySecret;
    }
    

    ossUtil类

     package com.example.upload.util;
    
    import com.aliyun.oss.OSSClient;
    import com.aliyun.oss.model.OSSObjectSummary;
    import com.aliyun.oss.model.ObjectListing;
    import com.example.upload.vo.OSSConfig;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.annotation.PostConstruct;
    import java.io.*;
    
    @Component
    public class OSSUtil {
    
        @Autowired
        private OSSConfig ossConfig;
    
        public static OSSUtil ossUtil;
    
        @PostConstruct
        public void init() {
            ossUtil = this;
        }
    
        //获取OSSClient实例
        private static OSSClient getOSSClient(){
            OSSClient ossClient = new OSSClient(ossUtil.ossConfig.getEndpoint(), ossUtil.ossConfig.getAccessKeyId(), ossUtil.ossConfig.getAccessKeySecret());
            return ossClient;
        }
    
        //文件流上传
        public static void uploadFile( File file,String key ) throws FileNotFoundException {
            OSSClient ossClient = getOSSClient();
            // 上传文件流
            //InputStream inputStream = new FileInputStream(fileName);
            ossClient.putObject(ossUtil.ossConfig.getBucketName(), key , file);
            // 关闭client
            ossClient.shutdown();
        }
    
        // MultipartFile方式上传
        public static String uploadPic(MultipartFile file, String key) throws IOException {
            OSSClient ossClient = getOSSClient();
            ossClient.putObject(ossUtil.ossConfig.getBucketName(), key , file.getInputStream());
            ossClient.shutdown();
            return key;
        }
    
        // 删除文件
        public static void deleteFile(String key ){
    
            // 创建OSSClient实例
            OSSClient ossClient = getOSSClient();
            // 删除Object
            ossClient.deleteObject(ossUtil.ossConfig.getBucketName(), key);
            // 关闭client
            ossClient.shutdown();
    
        }
    
        //获取存储对象的名字
        public static void listObject(){
            OSSClient ossClient = getOSSClient();
            // 获取指定bucket下的所有Object信息
            ObjectListing listing = ossClient.listObjects(ossUtil.ossConfig.getBucketName());
    
            // 遍历所有Object
            for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
                System.out.println(objectSummary.getKey());
            }
        }
    }
    
    

    测试controller类

    package com.example.upload.img;
    
    
    import com.example.upload.util.OSSUtil;
    import com.example.upload.util.UploadUtil;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    
    
    @Controller
    public class ImgUpload {
    
        @RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
        public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file,
                         HttpServletRequest request) throws IOException {
            String contentType = file.getContentType();
            String fileName = file.getOriginalFilename();
            String newFileName = UploadUtil.getRandomFileName(fileName);
            System.out.println("fileName-->" + newFileName);
            System.out.println("getContentType-->" + contentType);
            //String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
            String filePath = "C:\\test/";
            System.out.println(filePath);
            String key = OSSUtil.uploadPic(file,newFileName);
            System.out.println(key+"---------");
            if(key==null){
                return null;
            }
            //返回json
            return "uploadimg success";
        }
        @RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
        public String goUploadImg() {
            //跳转到 templates 目录下的 uploadimg.html
            return "uploadimg";
        }
    
    
    
    }
    
    

    测试html

    <!DOCTYPE html>
    <html>
    <head>
        <title>uploadimg.html</title>
    
        <meta name="keywords" content="keyword1,keyword2,keyword3"></meta>
        <meta name="description" content="this is my page"></meta>
        <meta name="content-type" content="text/html; charset=UTF-8"></meta>
    
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    
    </head>
    
    <body>
    <form enctype="multipart/form-data" method="post" action="/testuploadimg">
        图片<input type="file" name="file"/>
        <input type="submit" value="上传"/>
    </form>
    </body>
    </html>
    

    maven 依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>2.8.2</version>
            </dependency>
            
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.3</version>
            </dependency>
    
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.10</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    相关文章

      网友评论

          本文标题:spring boot 文件上传到阿里云OSS

          本文链接:https://www.haomeiwen.com/subject/oppgdftx.html