1. 程式人生 > >Java Springboot結合FastDFS實現檔案上傳以及根據圖片url將圖片上傳至圖片伺服器

Java Springboot結合FastDFS實現檔案上傳以及根據圖片url將圖片上傳至圖片伺服器

上一篇文章我們已經講解了如何搭建FastDFS圖片伺服器,環境我們準備好了現在就讓我們開始與Java結合將他應用到實際的專案中吧。本篇文章我們將會展示上傳圖片到FastDFS圖片伺服器以及通過外網的圖片url將圖片上傳至我們自己的圖片伺服器中。

1.建立springboot專案

首先我們建立一個最基礎的springboot專案,如果不瞭解springboot的同學可以先閱讀 java 搭建基於springboot的ssh(spring + springmvc + hibernate)的gradle專案(基礎篇),我們建立好的專案結構如下
這裡寫圖片描述

2.fastDFS所需配置

2.1 gradle配置

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot' group = 'com.beyondli' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { //fastdfs compile("com.github.tobato:fastdfs-client:1.25.2-RELEASE") //web層,保障專案可以持續啟動 compile('org.springframework.boot:spring-boot-starter-web'
) //url圖片轉換使用 compile group: 'commons-io', name: 'commons-io', version: '2.6' compileOnly('org.projectlombok:lombok') testCompile('org.springframework.boot:spring-boot-starter-test') }

2.2 FastdfsApplication(開關)

在開關檔案中新增配置
@Import(FdfsClientConfig.class)
這裡寫圖片描述

3.程式碼

3.1 上傳圖片到圖片伺服器


    // 上傳圖片流
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(MultipartFile file) throws Exception {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), "png",null);
        System.out.println(storePath.getFullPath());
        return storePath.getFullPath();
    }

3.2 根據外網圖片url將圖片上傳至自己的圖片伺服器

    // 上傳圖片
    @RequestMapping(value = "/csupload", method = RequestMethod.GET)
    public String csUpload() throws Exception {
        URL url = new URL("http://h5-statis.fenghuangyouxuan.com/jpg/b8deaa8d522c44ae8ba2573e3aa903b6.jpg?Expires=3088850901&AccessKey=9BBC935E745FEB1053F562620A0DC3C6&Signature=EmCFZ7ruAXoKFGStNBTVHpYlixY%3D");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //通過輸入流獲取圖片資料
        InputStream inputStream = conn.getInputStream();
        //注!圖片的大小如果錯誤則會出現圖片上傳成功但檢視圖片時圖片部分丟失的情況
        StorePath storePath= fastFileStorageClient.uploadFile(inputStream, IOUtils.toByteArray(url).length,"png",null);
        System.out.println(storePath.getFullPath());
        inputStream.close();
        return storePath.getFullPath();
    }

3.3整體程式碼

package com.beyondli.rest;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by beyondLi
 * Date 2017/12/1
 * Desc .
 */
@RestController
@RequestMapping(value = "/csfastdfs")
public class FastRest {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FastFileStorageClient storageClient;
    // 上傳圖片
    @RequestMapping(value = "/csupload", method = RequestMethod.GET)
    public String csUpload() throws Exception {
        URL url = new URL("http://h5-statis.fenghuangyouxuan.com/jpg/b8deaa8d522c44ae8ba2573e3aa903b6.jpg?Expires=3088850901&AccessKey=9BBC935E745FEB1053F562620A0DC3C6&Signature=EmCFZ7ruAXoKFGStNBTVHpYlixY%3D");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //通過輸入流獲取圖片資料
        InputStream inputStream = conn.getInputStream();
        //注!IOUtils.toByteArray(url).length的大小如果錯誤則會出現圖片上傳成功但檢視圖片時圖片部分丟失的情況
        StorePath storePath= fastFileStorageClient.uploadFile(inputStream, IOUtils.toByteArray(url).length,"png",null);
        System.out.println(storePath.getFullPath());
        inputStream.close();
        return storePath.getFullPath();
    }

    // 上傳圖片流
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(MultipartFile file) throws Exception {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), "png",null);
        System.out.println(storePath.getFullPath());
        return storePath.getFullPath();
    }
}

其實這篇文章也只是做一下記錄,技術含量並不是很高,希望如果有正在使用這個技術的同學可以拿來就直接使用,也是比較方便的。
原始碼下載地址
http://download.csdn.net/download/liboyang71/10167156