OSS,阿里云对象存储,这次项目中的图片都选择了这个存储方式。
因为做微信登录,所以在拿到用户头像的时候需要保持到oss,以免用户换头像后导致系统图片失效,影响用户的体验。
大体思路如下:
1.获取微信头像链接
2.将该图片写入本地
3.把刚刚写入的图片上传到oss
4.删除本地图片
上传oss的代码:
public static String uploadHeadImg(String url) throws Exception {
String imageName = imageName() + ".jpg"; //图片名称
String path=new File("").getAbsolutePath()+"/";<span style="white-space:pre"> </span>//这种方式可以拿到tomcat下的bin目录
client = new OSSClient(endpoint, accessKeyId, accessKeySecret); //初始化上传客户端
download(url, imageName, path); //下载图片到本地(见下面)
File file = new File(path + imageName); //根据下载的文件创建新文件
//文件不存在,则上传默认头像
if (!file.exists()) {
return ResourceUtil.getConfigByName("defaultphotourl");
}
InputStream content = new FileInputStream(file);
ObjectMetadata meta = new ObjectMetadata(); // 创建上传Object的Metadata
meta.setContentLength(file.length()); // 必须设置ContentLength
String keySuffixWithSlash = folderNameYM() + "/" + folderNameD() + "/"; // 上传Object.(这里的方法是生成根据时间生成文件夹名称的)
client.putObject(bucketName, keySuffixWithSlash + imageName, content, meta); //图片上传到oss
String ossPath = ResourceUtil.getConfigByName("oss-domain") + keySuffixWithSlash + imageName; //图片上传路径
if (file.exists()) {<span style="white-space:pre"> </span>//上传完成后,删除文件
file.delete();
}
content.close();<span style="white-space:pre"> </span>//关闭所有链接
client.shutdown();
return ossPath;
}下载网络图片的代码:
/**
* 下载图片到本地
*
* @param urlString
* @param filename
* @param savePath
* @throws Exception
*/
public static void download(String urlString, String filename, String savePath) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为5s
con.setConnectTimeout(5 * 1000);
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
OutputStream os = new FileOutputStream(sf.getPath() + "/" + filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}获取当前时间的年月组合和当前时间的日,我们用这种方式命名文件夹,大家可以参考下:
/**
* 文件夹名称-年月
*
* @return
*/
public static String folderNameYM() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
String strDate = formatter.format(new Date());
return strDate;
}
/**
* 文件夹名称-天
*
* @return
*/
public static String folderNameD() {
SimpleDateFormat formatter = new SimpleDateFormat("dd");
String strDate = formatter.format(new Date());
return strDate;
}图片的命名方法,按照当前毫秒数加随机数
/**
* 图片名称生成
*
* @return
*/
public static String imageName() {
Random random = new Random();//生成随机数
String strDate = Long.toString(System.currentTimeMillis());
for (int i = 0; i < 3; i++) {
strDate = strDate + random.nextInt(9);
}
return strDate;
}
ps:这里提到oss了,忘记说它的一个优点了,我觉得挺好的,共享下。
我们可以根据自己的需要选择图片的大小,相当于我们上传了一张图片图片,就有了100张图片。
http://image-demo.img-cn-hangzhou.aliyuncs.com/example.jpg@200h
后面的200h代表高度200px,高度根据比例变,还有其他改变方式,大家自行研究https://help.aliyun.com/document_detail/oss/oss-img-guide/resize/resize.html?spm=5176.docoss/product-documentation/intruduction.6.467.xokMaA
本文介绍了如何使用Java将微信登录获取的用户头像保存至阿里云OSS,以确保用户更换头像后系统图片不失效。步骤包括获取微信头像链接、保存至本地、上传OSS及删除本地图片。OSS的一个亮点是可以根据需求调整图片大小,如通过URL参数设置图片高度。
1086

被折叠的 条评论
为什么被折叠?



