修改个人信息头像上传
This commit is contained in:
parent
98fc694b9e
commit
24d978b6f3
@ -83,6 +83,12 @@
|
||||
<artifactId>xhpc-common</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.10.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -1,8 +1,15 @@
|
||||
package com.xhpc.system.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.xhpc.common.core.utils.DateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
@ -26,6 +33,9 @@ import com.xhpc.system.api.domain.SysFile;
|
||||
import com.xhpc.system.api.domain.SysUser;
|
||||
import com.xhpc.system.api.model.LoginUser;
|
||||
import com.xhpc.system.service.ISysUserService;
|
||||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 个人信息 业务处理
|
||||
@ -44,6 +54,8 @@ public class SysProfileController extends BaseController
|
||||
|
||||
@Autowired
|
||||
private RemoteFileService remoteFileService;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
/**
|
||||
* 个人信息
|
||||
@ -124,19 +136,25 @@ public class SysProfileController extends BaseController
|
||||
*/
|
||||
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
|
||||
@PostMapping("/avatar")
|
||||
public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
|
||||
{
|
||||
if (!file.isEmpty())
|
||||
{
|
||||
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
R<SysFile> fileResult = remoteFileService.upload(file);
|
||||
if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
|
||||
{
|
||||
return AjaxResult.error("文件服务异常,请联系管理员");
|
||||
public AjaxResult avatar(HttpServletRequest request) {
|
||||
try {
|
||||
String fileNames = "";
|
||||
StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
|
||||
Iterator<String> iterator = req.getFileNames();
|
||||
while (iterator.hasNext()) {
|
||||
MultipartFile file = req.getFile(iterator.next());
|
||||
fileNames = file.getOriginalFilename();
|
||||
fileNames = DateUtils.timePath() + fileNames;
|
||||
InputStream input = file.getInputStream();
|
||||
// 创建OSSClient实例
|
||||
OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
|
||||
// 上传文件流
|
||||
ossClient.putObject(environment.getProperty("oss.bucket-name"), fileNames, input);
|
||||
ossClient.shutdown();
|
||||
}
|
||||
String url = fileResult.getData().getUrl();
|
||||
if (userService.updateUserAvatar(loginUser.getUsername(), url))
|
||||
{
|
||||
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
String url="https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"+fileNames;
|
||||
if (userService.updateUserAvatar(loginUser.getUsername(), url)) {
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("imgUrl", url);
|
||||
// 更新缓存用户头像
|
||||
@ -144,6 +162,8 @@ public class SysProfileController extends BaseController
|
||||
tokenService.setLoginUser(loginUser);
|
||||
return ajax;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return AjaxResult.error("上传图片异常,请联系管理员");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user