发表于: 2017-12-07 19:53:32

1 821


今天完成的事

【1】完成图片上传和存储

没啥好讲的,就是用了用了工具类之后成功~

用了七牛云和阿里云。

贴贴工具类吧。

package com.xiuzhen.API;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.swing.*;

/**
* Created by ${MIND-ZR} on 2017/12/7.
*/
@Service
public class QiNiuUpLoad {
Logger logger = Logger.getLogger(QiNiuUpLoad.class);

   public void picUp(String picturepath) {
logger.info("进入到七牛云上传方法。");
       try {
/* UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
           JFileChooser fileChooser = new JFileChooser();
           fileChooser.showOpenDialog(null);
           String filePath = "" + fileChooser.getSelectedFile();
           String fileName = "" + fileChooser.getSelectedFile().getName();
           System.out.println("选择文件路径: " + filePath);
           System.out.println("选择文件的文件名: " + fileName);*/


           //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
           //JFileChooser fileChooser = new JFileChooser();
           //fileChooser.showOpenDialog(null);
           String filePath = picturepath;
           //String fileName = "" + fileChooser.getSelectedFile().getName();
           System.out.println("选择文件路径: " + filePath);
           //System.out.println("选择文件的文件名: " + fileName);
           logger.info("路径转换完成");

           String newURL = filePath.replaceAll("\\\\", "/");
           System.out.println("转义后的文件路径:" + newURL);
           //构造一个带指定Zone对象的配置类
           Configuration cfg = new Configuration(Zone.zone1());
           //...其他参数参考类注释
           UploadManager uploadManager = new UploadManager(cfg);
           //...生成上传凭证,然后准备上传
           String accessKey = "2ZSpt7Zirvy5naiK9458PNXT3z4YbQFNnJFZLSBo";
           String secretKey = "Bb51CNaB4r_e0K3wN-QQaKYK3tXfD7nTW0TfEKaW";
           String bucket = "mind22";
           //如果是Windows情况下,格式是 D:\\qiniu\\test.png
           String localFilePath = newURL;
           //默认不指定key的情况下,以文件内容的hash值作为文件名
           String key = null;
           Auth auth = Auth.create(accessKey, secretKey);
           String upToken = auth.uploadToken(bucket);
           try {
Response response = uploadManager.put(localFilePath, key, upToken);
               //解析上传成功的结果
               DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
               System.out.println(putRet.key);
               System.out.println(putRet.hash);
           } catch (QiniuException ex) {
Response r = ex.response;
               System.err.println(r.toString());
               try {
System.err.println(r.bodyString());
               } catch (QiniuException ex2) {
//ignore
               }
}
} catch (Exception e) {
System.out.println("七牛云上传图片模块有错误");
           e.printStackTrace();
       }
}
}


上传成功~

然后是阿里。

package com.xiuzhen.API;

import com.aliyun.oss.OSSClient;
import org.springframework.stereotype.Service;

import java.io.File;

/**
* Created by ${MIND-ZR} on 2017/12/7.
*/
@Service
public class AliUpLoad {
public void AliUpload(){
// endpoint以杭州为例,其它region请按实际情况填写
       String endpoint = "http://oss-cn-beijing.aliyuncs.com";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
       String accessKeyId = "LTAIZyl7AnHs699l";
       String accessKeySecret = "xrDdkEtvW1aKSo8tnRCQxrUPz6Ojhg";
// 创建OSSClient实例
       OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

       ossClient.putObject("mind222", accessKeyId, new File("C:/Users/sony/Desktop/111.png"));


// 关闭client
       ossClient.shutdown();
   }
}



遇到的问题

阿里的权限配置导致的,说来话长。

阿里的权限配置还挺麻烦的。


收获

熟悉了文件上传接口

明天完成的事

完成任务七

禅道链接

http://task.ptteng.com/zentao/my-task-assignedTo.html


返回列表 返回列表
评论

    分享到