第三方API之图片上传


第三方图片上传_腾讯视频

视频:https://v.qq.com/x/page/m052917m1ia.html

PPT:https://github.com/WuZhiyong1759/xiaoketangPPT/tree/master/XKT2017%E5%B9%B47%E6%9C%8825%E6%97%A5PPT

1.背景介绍

在程序开发过程中,我们总是会碰到需要图片管理相关的功能,如更改头像,相册上传图片呀,网站的banner图的后台管理呀等等。那么这个图片具体是怎么上传的呢?

2.知识剖析

本次课程内容将使用七牛云的对象存贮

3.常见问题

实现图片上传的代码是怎样的呢?

4.解决方案

查看七牛云的官方API文档

5.编码实

----------------------------------------------------------------------控制器---------------------------------------------------------------------

@RequestMapping("/upload/images")

publicStringupLoadImages(String file)throwsClassNotFoundException,UnsupportedLookAndFeelException,InstantiationException,IllegalAccessException {

imagesUtil.UpLoadImgs(file);

return"home";

}

@RequestMapping("/delete/images")

publicStringdeleteImages(String file){

imagesUtil.DeleteImgs(file);

return"login";

}

-----------------------------------------------------封装了上传和删除功能的类---------------------------------------------------------------

public classImagesUtil {

private staticStringaccessKey=null;

private staticStringsecretKey=null;

private staticStringbucket=null;

private staticConfigurationcfg;

publicImagesUtil(String accessKey,String secretKey,String bucket){

this.accessKey=accessKey;

this.secretKey=secretKey;

this.bucket=bucket;

//构造一个带指定Zone对象的配置类

this.cfg=newConfiguration(Zone.zone2());

}

public  voidUpLoadImgs(String file)throwsClassNotFoundException,UnsupportedLookAndFeelException,InstantiationException,IllegalAccessException {

//打开文件选择框,

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

JFileChooser fc=newJFileChooser();

fc.showOpenDialog(null);

String filePath=""+fc.getSelectedFile();

System.out.println("选择的文件路径:"+filePath);//输出文件路径

String newURL = filePath.replaceAll("\\\\","\\\\"+"\\\\");

System.out.println("转义后的文件路径:"+newURL);

//...其他参数参考类注释

UploadManager uploadManager =newUploadManager(cfg);

//...生成上传凭证,然后准备上传

//如果是Windows情况下,格式是D:\\qiniu\\test.png

String localFilePath = newURL;

//默认不指定key的情况下,以文件内容的hash值作为文件名

String key = file;

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

String upToken = auth.uploadToken(bucket);

try{

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

System.out.println("上传文件,状态码:"+response.statusCode);

//解析上传成功的结果

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

System.out.println(putRet.key);

System.out.println(putRet.hash);

System.out.println("图片外链:"+"http://otjrc4xc4.bkt.clouddn.com/"+key);

}catch(QiniuException ex) {

Response r = ex.response;

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

try{

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

}catch(QiniuException ex2) {

//ignore

}

}

}

public  voidDeleteImgs(String fileName){

String key = fileName;

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

BucketManager bucketManager =newBucketManager(auth,cfg);

try{

Response response=bucketManager.delete(bucket,key);

System.out.println("删除文件状态码:"+response.statusCode);

}catch(QiniuException ex) {

//如果遇到异常,说明删除失败

System.err.println(ex.code());

System.err.println(ex.response.toString());

}

}

}

-----------------------------------------------------------------spring中配置---------------------------------------------------------------------

-->



6.扩展思考

图片的删除怎么做

7.参考文献

https://www.qiniu.com/


友情连接:          IT修真院官网              IT修真院Java简书专栏

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,092评论 19 139
  • 最近做项目用到了七牛云存储,就讲一下如何使用springMVC对视频上传到七牛云上。 Java SDK依赖的第三方...
    远向阅读 9,915评论 1 3
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,216评论 18 399
  • 1.场景还原 近期,由于项目需求需要上传图片logo,笔者在探索的过程中遇到过很多坑,今晚就把个人经验分享出来尽量...
    老张家的独苗阅读 7,036评论 0 2
  • 暑热难耐,不妨走出钢筋水泥堆砌的城市,走进古老而宁静的杭城古道,去看青葱的山林、去听潺潺的流水和阵阵虫鸣。今天放假...
    放假周边游阅读 3,720评论 0 1

友情链接更多精彩内容