springboot将图片上传至七牛云服务器(超详细) | 您所在的位置:网站首页 › 图片上传网站不占用服务器 › springboot将图片上传至七牛云服务器(超详细) |
我们在公司的业务开发中,经常会接触到图片上传功能,有很多小伙伴都会在这里遇坑,特别是将图片上传至七牛云服务器上,网上的很多讲解也都很不明确。那么今天我们就要来讲一讲如何将本地的图片上传至七牛云服务器上,配合步骤自己做一个小的demo吧!本文章很适合新手入门学习哦! 一.注册七牛云账号首先我们需要申请一个七牛云账号,登录七牛云网站进行注册,注册成功后登录并进入管理控制台 项目方面我们使用的是当今比较流行的springboot搭建,springboot项目还不熟悉的小伙伴可以先去补习一下,非常简单,如果有需要也可以留言,我会写一篇springboot项目搭建的过程,此文章就不多赘述了,直接开始项目。 首先,我们复制七牛云的相关maven依赖到pom.xml中。 com.qiniu qiniu-java-sdk [7.2.0, 7.2.99] com.squareup.okhttp3 okhttp 3.3.1 compile com.google.code.gson gson 2.6.2 compile com.qiniu happy-dns-java 0.1.4 compile然后我们在项目配置文件中编写我们七牛云的配置,我这里使用的是application.yml,使用application.properties的请自行调整,具体看图
baseUploadUrl是我自定义的本地文件路径,七牛云在图片上传时会在本地临时存储一个文件,可以自己在配置文件中定义,不需要和我的一样。 如果以上步骤都做完并且没问题的话,那么我们的基本配置就全部准备完成了,下面我们开始service的开发。 三.图片上传开发首先我们在service目录下新建FileService接口 package com.cpj.service.upload; import com.qiniu.common.QiniuException; import java.io.File; import java.util.Map; public interface FileService { /** * @Author : * @Description : //TODO 多文件上传 * @Date : 10:04 2019/4/3 * @Param : * @return : **/ Map uploadFile(File file) throws QiniuException; }并且新建该接口的实现类 package com.cpj.service.upload; import com.google.gson.Gson; import com.qiniu.common.QiniuException; import com.qiniu.http.Response; import com.qiniu.storage.UploadManager; import com.qiniu.storage.model.DefaultPutRet; import com.qiniu.util.Auth; import com.qiniu.util.StringMap; 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.util.HashMap; import java.util.Map; /** * @author : * @ClassName :FileServiceImpl * @date : 2019/4/3 10:05 * @description : TODO */ @Service public class FileServiceImpl implements FileService { @Autowired private UploadManager uploadManager; @Autowired private Auth auth; @Value("${qiniu.bucket}") private String bucket; private StringMap putPolicy; @Override public Map uploadFile(File file) throws QiniuException { Map map = new HashMap(); Response response = this.uploadManager.put(file,null,getUploadToken()); //解析上传的结果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(),DefaultPutRet.class); String imageName = putRet.hash; int retry = 0; while(response.needRetry() && retry < 3){ response = this.uploadManager.put(file,null,getUploadToken()); } map.put("response",response); map.put("imgName",imageName); return map; } private String getUploadToken(){ return this.auth.uploadToken(bucket,null,3600,putPolicy); } }将以上代码全部复制到自己的项目当中,图片上传的service层就完成了,一点也不复杂,直接看就可以看懂,所以我这里也不多废话了,接下来我们写controller层的代码。 package com.cpj.controller; import com.cpj.service.upload.FileService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * @author : * @ClassName :UploadController * @date : 2019/4/1 17:47 * @description : TODO */ @RestController @RequestMapping("/upload") @Api(tags = "图片上传接口") public class UploadController { @Autowired FileService fileService; @Value("${baseUploadUrl}") private String url; @PostMapping(value = "/uploadImg") @ApiOperation(value = "单个图片上传到七牛云") public Map uploadImg(@RequestParam(value = "file")MultipartFile upfile) throws IOException { Map map = new HashMap(); String fileName = upfile.getOriginalFilename(); File file = new File(url + fileName); try{ //将MulitpartFile文件转化为file文件格式 upfile.transferTo(file); Map response = fileService.uploadFile(file); Object imageName = response.get("imgName"); map.put("url",imageName); map.put("state","SUCESS"); }catch (Exception e){ e.printStackTrace(); } return map; } }controller层的代码其实可以封装到service中,但是我这里为了区分作用,就直接写在这里了,map中的url就是我们上传到七牛云之后我们图片的名字,通过我们之前的域名+这个名字就可以进行访问了。 如果都编写完了,我们可以用swagger进行测试看看功能是否正常,swagger的用法这里不多赘述了,感兴趣的小伙伴快去补习补习吧,真的很好用哦。 到此为止所有的上传图片的功能就结束了,不知道这篇文章有没有帮助到你呢? |
CopyRight 2018-2019 实验室设备网 版权所有 |