Java Springboot结合FastDFS实现文件上传以及根据图片url将图片上传至图片服务器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liboyang71/article/details/78860074

上一篇文章我们已经讲解了如何搭建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

猜你喜欢

转载自blog.csdn.net/liboyang71/article/details/78860074