发表于: 2017-11-11 23:18:33
1 851
今天完成的事
主要完成的就是使用图片上传功能的完善
使用springmvc上传图片
1)先在pom.xml添加如下jar包
.png)

2)然后是在serlvet.xml中添加对mutilpart的支持
.png)

3)然后是工具类方法:
.png)

4)对应的jsp:
.png)

还有就是利用配置文件来完成
package com.ptteng.util;
import com.aliyun.oss.OSSClient;
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.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class HeadUpload {
private String num;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public void upload(MultipartFile file, String fileName) throws FileNotFoundException, IOException {
if (this.num == "1") {
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "";
String secretKey = "";
String bucket = "manatsu123";
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = fileName;
try {
byte[] uploadBytes = file.getBytes();
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(uploadBytes, 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
}
}
} else {
String accessKeyId = "";
String accessKeySecret = "";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
byte[] content = file.getBytes();
ossClient.putObject("manatsu123", fileName, new ByteArrayInputStream(content));
ossClient.shutdown();
}
}
}
为了保护密钥把关于密钥的部分删掉了
参考成延师兄的部分
当数字为1的时候,使用七牛上传,当为其他数字的时候,使用阿里云上传
.png)

金山云我也试着学习了一下,没搞成功,学习成本高所以暂时放弃,改用阿里云了
.png)
明天计划的事
邮箱功能完善,做数据迁移
遇到的问题
代码和思路都比较乱,等提交任务的时候得好好捋一遍
收获
1.了解使用Spring上传文件的方法
2.对七牛云和阿里云的SDK会用一些了
评论