视频: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专栏