修复微信公众号登录没有昵称头像问题

This commit is contained in:
TinyAnts 2023-03-30 11:35:52 +08:00
parent 66e4f40bb3
commit 4ad7a7b785
2 changed files with 53 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package com.mdd.common.util;
import com.mdd.common.config.GlobalConfig; import com.mdd.common.config.GlobalConfig;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -130,30 +131,61 @@ public class ToolUtils {
* 下载文件 * 下载文件
* *
* @author fzr * @author fzr
* @param urlString (文件网址) * @param urlStr (文件网址)
* @param savePath (保存路径,: /www/uploads) * @param savePath (保存路径,: /www/uploads/aa.png)
* @param filename (保存名称,: aa.png)
* @throws IOException IO异常 * @throws IOException IO异常
*/ */
public static void download(String urlString, String savePath, String filename) throws IOException { public static void download(String urlStr, String savePath) throws IOException {
URL url = new URL(urlString); ByteArrayOutputStream bos = null;
URLConnection con = url.openConnection(); FileOutputStream fos = null;
con.setConnectTimeout(20 * 1000); InputStream inputStream = null;
File sf = new File(savePath); try {
if (!sf.exists()) { URL url = new URL(urlStr);
if (sf.mkdirs()) { HttpURLConnection conn = (HttpURLConnection) url.openConnection();
throw new IOException("创建目录失败"); conn.setConnectTimeout(5*1000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
inputStream = conn.getInputStream();
// 获取数组数据
byte[] buffer = new byte[4*1024];
int len;
bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
} }
} byte[] getData = bos.toByteArray();
try (InputStream in = con.getInputStream();
OutputStream out = new FileOutputStream(sf.getPath() + "\\" + filename)) { // 新创建文件夹
byte[] buff = new byte[1024]; String fileName = StringUtils.substringAfterLast(savePath, "/");
int n; String path = savePath.replace("/"+fileName, "");
while ((n = in.read(buff)) >= 0) { File saveDir = new File(path);
out.write(buff, 0, n); if(!saveDir.exists()) {
if (!saveDir.mkdirs()) {
throw new IOException("创建存储文件夹失败");
}
} }
// 保存文件数据
File file = new File(savePath);
fos = new FileOutputStream(file);
fos.write(getData);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); throw new IOException(e.getMessage());
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException ignored) {}
}
if (fos != null) {
try {
fos.close();
} catch (IOException ignored) {}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) {}
}
} }
} }

View File

@ -341,7 +341,8 @@ public class LoginServiceImpl implements ILoginService {
String date = TimeUtils.millisecondToDate(time, "yyyyMMdd"); String date = TimeUtils.millisecondToDate(time, "yyyyMMdd");
String name = ToolUtils.makeMd5(ToolUtils.makeUUID()+time) + ".jpg"; String name = ToolUtils.makeMd5(ToolUtils.makeUUID()+time) + ".jpg";
String path = "avatar" + date + "/" + name; String path = "avatar" + date + "/" + name;
ToolUtils.download(avatar, YmlUtils.get("like.upload-directory"), path); String savePath = YmlUtils.get("like.upload-directory") + path;
ToolUtils.download(avatar, savePath);
defaultAvatar = path; defaultAvatar = path;
} catch (IOException ignored) {} } catch (IOException ignored) {}
} }