批量下载线上人员头像到本地F:\\Users\\Images 文件夹下
This commit is contained in:
parent
6aca092e12
commit
40b2f45e66
|
|
@ -24,6 +24,7 @@ import com.bonus.system.api.domain.SysUser;
|
|||
import com.bonus.system.api.model.LoginUser;
|
||||
import com.bonus.system.domain.UserPasswordHistory;
|
||||
import com.bonus.system.service.*;
|
||||
import com.bonus.system.utils.FileCommonUtils;
|
||||
import com.bonus.system.warning.WebSocketHandler;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -96,6 +97,18 @@ public class SysUserController extends BaseController {
|
|||
return getDataTableError(new ArrayList<>());
|
||||
}
|
||||
|
||||
@PostMapping("/downloadUserPhoto")
|
||||
@SysLog(title = "用户管理", businessType = OperaType.QUERY, logType = 0, module = "系统管理->用户管理", details = "查询用户列表")
|
||||
public AjaxResult downloadUserPhoto() {
|
||||
try {
|
||||
userService.downloadUserPhoto();
|
||||
return success();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.toString(), e);
|
||||
}
|
||||
return error();
|
||||
}
|
||||
|
||||
@RequiresPermissions("system:user:export")
|
||||
@PostMapping("/export")
|
||||
@SysLog(title = "用户管理", businessType = OperaType.EXPORT, logType = 0, module = "系统管理->用户管理", details = "导出用户信息")
|
||||
|
|
@ -304,7 +317,8 @@ public class SysUserController extends BaseController {
|
|||
if (StringUtils.isNotNull(userId)) {
|
||||
SysUser sysUser = userService.selectUserById(userId);
|
||||
sysUser.setPassword(null);
|
||||
sysUser.setPhotoUrl(remoteFileService.getFullFileUrl(sysUser.getPhotoUrl()));
|
||||
// sysUser.setPhotoUrl(remoteFileService.getFullFileUrl(sysUser.getPhotoUrl()));
|
||||
sysUser.setPhotoUrl(remoteFileService.getFullFileUrl(FileCommonUtils.getFullFileUrl(sysUser.getPhotoUrl())));
|
||||
ajax.put(AjaxResult.DATA_TAG, sysUser);
|
||||
ajax.put("postIds", postService.selectPostListByUserId(userId));
|
||||
ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
|
||||
|
|
|
|||
|
|
@ -246,4 +246,6 @@ public interface ISysUserService {
|
|||
|
||||
|
||||
SysUser getDeviceStaffInfoBySn(String photoNumber);
|
||||
|
||||
void downloadUserPhoto();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package com.bonus.system.service.impl;
|
||||
|
||||
import com.bonus.common.core.constant.Constants;
|
||||
import com.bonus.common.core.constant.UserConstants;
|
||||
import com.bonus.common.core.domain.R;
|
||||
import com.bonus.common.core.enums.UserPermanentEnum;
|
||||
|
|
@ -41,6 +40,13 @@ import javax.annotation.Resource;
|
|||
import javax.validation.Validator;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 用户 业务层处理
|
||||
|
|
@ -135,6 +141,152 @@ public class SysUserServiceImpl implements ISysUserService {
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadUserPhoto() {
|
||||
SysUser u = new SysUser();
|
||||
List<SysUser> userList = this.selectUserList(u);
|
||||
// 保存目录
|
||||
String saveDir = "F:\\Users\\Images";
|
||||
batchDownloadImages(userList,saveDir);
|
||||
}
|
||||
|
||||
public static void batchDownloadImages(List<SysUser> userList, String saveDir) {
|
||||
int successCount = 0;
|
||||
int totalCount = userList.size();
|
||||
|
||||
System.out.println("开始批量下载,共" + totalCount + "个图片...");
|
||||
|
||||
for (int i = 0; i < totalCount; i++) {
|
||||
String url = userList.get(i).getPhotoUrl();
|
||||
if(url ==null){
|
||||
continue;
|
||||
}else{
|
||||
url = url.replace("http://192.168.20.242:9000/","http://192.168.20.234:9090/");
|
||||
}
|
||||
String fileName = "image_" + userList.get(i).getUserId()+"_"+userList.get(i).getNickName(); // 生成文件名
|
||||
|
||||
if (downloadImage(url, saveDir, fileName)) {
|
||||
successCount++;
|
||||
}
|
||||
|
||||
// 避免请求过快,可根据需要调整
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("下载完成!成功: " + successCount + "/" + totalCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载单个图片
|
||||
* @param imageUrl 图片URL地址
|
||||
* @param saveDir 保存目录
|
||||
* @param fileName 保存的文件名(不含扩展名)
|
||||
* @return 下载成功返回true,否则返回false
|
||||
*/
|
||||
public static boolean downloadImage(String imageUrl, String saveDir, String fileName) {
|
||||
try {
|
||||
// 创建URL对象
|
||||
URL url = new URL(imageUrl);
|
||||
|
||||
// 打开连接
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setReadTimeout(10000);
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
|
||||
// 获取文件扩展名
|
||||
String fileExtension = getFileExtension(imageUrl);
|
||||
if (fileExtension.isEmpty()) {
|
||||
// 尝试从Content-Type获取扩展名
|
||||
String contentType = connection.getContentType();
|
||||
fileExtension = getExtensionFromContentType(contentType);
|
||||
}
|
||||
|
||||
// 构建保存路径
|
||||
String savePath = saveDir + File.separator + fileName + fileExtension;
|
||||
|
||||
// 确保目录存在
|
||||
File dir = new File(saveDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 下载文件
|
||||
try (InputStream in = connection.getInputStream();
|
||||
FileOutputStream out = new FileOutputStream(savePath)) {
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
System.out.println("下载成功: " + fileName + fileExtension);
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("下载失败: " + fileName + " - " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Content-Type获取文件扩展名
|
||||
*/
|
||||
private static String getExtensionFromContentType(String contentType) {
|
||||
if (contentType == null) {
|
||||
return ".jpg"; // 默认扩展名
|
||||
}
|
||||
|
||||
switch (contentType.toLowerCase()) {
|
||||
case "image/jpeg":
|
||||
return ".jpg";
|
||||
case "image/png":
|
||||
return ".png";
|
||||
case "image/gif":
|
||||
return ".gif";
|
||||
case "image/bmp":
|
||||
return ".bmp";
|
||||
case "image/webp":
|
||||
return ".webp";
|
||||
default:
|
||||
return ".jpg";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从URL获取文件扩展名
|
||||
*/
|
||||
private static String getFileExtension(String url) {
|
||||
String[] imageExtensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"};
|
||||
|
||||
for (String ext : imageExtensions) {
|
||||
if (url.toLowerCase().contains(ext)) {
|
||||
return ext;
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从URL路径获取
|
||||
int lastSlash = url.lastIndexOf('/');
|
||||
int lastDot = url.lastIndexOf('.');
|
||||
|
||||
if (lastDot > lastSlash && lastDot > 0) {
|
||||
String possibleExt = url.substring(lastDot);
|
||||
// 检查是否是合理的图片扩展名
|
||||
if (possibleExt.length() <= 5) { // 扩展名通常不超过5个字符
|
||||
return possibleExt;
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public List<SysUser> selectUserList4Job(SysUser user) {
|
||||
return userMapper.selectUserList(user);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue