java實現七牛雲圖片文件的上傳

七牛雲:https://portal.qiniu.com/create#resource

首先需要去註冊一個賬號實現實名認證

之後打開七牛雲的

我們需要先創建一個儲存空間來給我們使用

這裏的key也需要記錄下來我們等會也會用的到

需要用到的參數:

1、AccessKey (在“個人中心”–>“祕鑰管理”中)

2、SecretKey (在“個人中心”–>“祕鑰管理”中)

3、存儲空間名字(這個是自己創建的)

之後就到我們的java代碼了

我們首先需要導入一些七牛雲的依賴包

 

[XML] 純文本查看 複製代碼

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<dependency>

    <groupId>com.qiniu</groupId>

    <artifactId>qiniu-java-sdk</artifactId>

    <version>[7.2.0, 7.2.99]</version>

</dependency>

 

<dependency>

    <groupId>com.squareup.okhttp3</groupId>

    <artifactId>okhttp</artifactId>

    <version>3.14.2</version>

    <scope>compile</scope>

</dependency>

<dependency>

    <groupId>com.google.code.gson</groupId>

    <artifactId>gson</artifactId>

    <version>2.8.5</version>

    <scope>compile</scope>

</dependency>

<dependency>

    <groupId>com.qiniu</groupId>

    <artifactId>happy-dns-java</artifactId>

    <version>0.1.6</version>

    <scope>test</scope>

</dependency>

<dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.12</version>

    <scope>test</scope>

</dependency>

 

我們從前端接收過來的圖片需要去接收

 

[Java] 純文本查看 複製代碼

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//處理文件上傳

  @Log("文件上傳")

  @RequestMapping(value = "uploadImg", method = RequestMethod.POST)

  public R uploadImg(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws JSONException {       

      String contentType = file.getContentType();

      //System.out.print(contentType);

      String fileName = System.currentTimeMillis()+file.getOriginalFilename();

      int code = 0;

      String filePath = "D:/E";

      //JSONObject jo = new JSONObject();//實例化json數據

      //Map<String,String> jo = new HashMap<>();

      if (file.isEmpty()) {  

          code = 1;

          fileName = "";

      }       

      try

         uploadFile(file.getBytes(), filePath, fileName); 

         code = 0;

      } catch (Exception e) { 

      // TODO: handle exception       

      }

      //返回json

      return R.ok().put("code",code).put("filename","/imctemp-rainy/"+fileName);   

  }

 

之後我們需要去調用一個方法去把我們的圖片拿到我們自己寫的這個路徑中

這裏我們呢做的操作是等我們的圖片複製完後才能之後放上七牛雲的上傳代碼

這裏我們需要去把我們的key和儲存空間名配置一下

然後在官方文檔中就給我寫好了

在這裏我就把代碼放一下

這樣我們的圖片上傳到七牛雲的過程就完了

去測試一下吧

[Java] 純文本查看 複製代碼

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {       

        File targetFile = new File(filePath);

        if (!targetFile.exists()) {

           targetFile.mkdirs();   

        }       

        FileOutputStream out = new FileOutputStream(filePath +"/"+ fileName);

        out.write(file);     

        out.flush();  

        out.close();

         Configuration cfg = new Configuration(Zone.zone1()); // zong1() 代表華北地區

            UploadManager uploadManager = new UploadManager(cfg);

 

            String accessKey = "這裏寫自己的"; // AccessKey的值

            String secretKey = "這裏寫自己的"; // SecretKey的值

            String bucket = "這裏寫自己的"; // 存儲空間名

            String localFilePath = filePath +"/"+ fileName; // 上傳圖片路徑

 

            String key = fileName; // 在七牛雲中圖片的命名

            Auth auth = Auth.create(accessKey, secretKey);

            String upToken = auth.uploadToken(bucket);

            try {

                Response response = uploadManager.put(localFilePath, key, upToken);

                // 解析上傳成功的結果

                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

                System.out.println(putRet.key);

                System.out.println(putRet.hash);

            } catch (QiniuException ex) {

                Response r = ex.response;

                System.err.println(r.toString());

                try {

                    System.err.println(r.bodyString());

                } catch (QiniuException ex2) {

                    // ignore

                }

            }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章