增加web演示
This commit is contained in:
parent
572ce3dfb2
commit
4e297ea584
|
@ -0,0 +1,55 @@
|
|||
package com.zheng.oss.web;
|
||||
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by shuzheng on 2016/11/12.
|
||||
*/
|
||||
public class QiniuDemo {
|
||||
//设置好账号的ACCESS_KEY和SECRET_KEY
|
||||
String ACCESS_KEY = "";
|
||||
String SECRET_KEY = "";
|
||||
//要上传的空间
|
||||
String bucketname = "zheng";
|
||||
//上传到七牛后保存的文件名
|
||||
String key = "my-java.png";
|
||||
//上传文件的路径
|
||||
String FilePath = "C:\\Users\\admin\\Pictures\\zsz\\20161108161228.png";
|
||||
|
||||
//密钥配置
|
||||
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
|
||||
//创建上传对象
|
||||
UploadManager uploadManager = new UploadManager();
|
||||
|
||||
//简单上传,使用默认策略,只需要设置上传的空间名就可以了
|
||||
public String getUpToken(){
|
||||
return auth.uploadToken(bucketname);
|
||||
}
|
||||
|
||||
public void upload() throws IOException {
|
||||
try {
|
||||
//调用put方法上传
|
||||
Response res = uploadManager.put(FilePath, key, getUpToken());
|
||||
//打印返回的信息
|
||||
System.out.println(res.bodyString());
|
||||
} catch (QiniuException e) {
|
||||
Response r = e.response;
|
||||
// 请求失败时打印的异常的信息
|
||||
System.out.println(r.toString());
|
||||
try {
|
||||
//响应的文本信息
|
||||
System.out.println(r.bodyString());
|
||||
} catch (QiniuException e1) {
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) throws IOException {
|
||||
new QiniuDemo().upload();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
package com.zheng.oss.web.controller;
|
||||
package com.zheng.oss.web.controller;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import com.zheng.oss.rpc.api.OssApiService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -11,20 +12,23 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
import java.io.ByteArrayInputStream;
|
||||
|
||||
/**
|
||||
* Created by shuzheng on 2016/11/12.
|
||||
* Created by shuzheng on 2017/4/18.
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/app")
|
||||
public class AppController {
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
private OssApiService ossApiService;
|
||||
|
||||
@RequestMapping(value = "/index", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public String index(ModelMap modelMap) {
|
||||
// endpoint以杭州为例,其它region请按实际情况填写
|
||||
String endpoint = "";
|
||||
String endpoint = "oss-cn-shanghai.aliyuncs.com";
|
||||
// accessKey请登录https://ak-console.aliyun.com/#/查看
|
||||
String accessKeyId = "";
|
||||
String accessKeySecret = "";
|
||||
String accessKeyId = "LTAIf5dDIthJN3h0";
|
||||
String accessKeySecret = "2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d";
|
||||
|
||||
// 创建OSSClient实例
|
||||
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
||||
|
@ -35,6 +39,7 @@ public class AppController {
|
|||
|
||||
// 关闭ossClient
|
||||
ossClient.shutdown();
|
||||
|
||||
return "success";
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
profile.env=dev
|
||||
|
||||
### aliyun ###
|
||||
aliyun.oss.endpoint=
|
||||
aliyun.oss.endpoint.internal=
|
||||
aliyun.oss.endpoint=http://shuzheng.oss-cn-shanghai.aliyuncs.com
|
||||
aliyun.oss.endpoint.internal=http://shuzheng.oss-cn-shanghai-internal.aliyuncs.com
|
||||
|
|
Loading…
Reference in New Issue