Commit e79abf81 authored by hezhen's avatar hezhen

修改图像

parent cf8d2be0
...@@ -27,6 +27,7 @@ import com.github.wxiaoqi.security.common.util.result.JsonResultUtil; ...@@ -27,6 +27,7 @@ import com.github.wxiaoqi.security.common.util.result.JsonResultUtil;
import com.xxfc.platform.activity.feign.ActivityFeign; import com.xxfc.platform.activity.feign.ActivityFeign;
import com.xxfc.platform.im.feign.ImFeign; import com.xxfc.platform.im.feign.ImFeign;
import com.xxfc.platform.universal.dto.SmsTemplateDTO; import com.xxfc.platform.universal.dto.SmsTemplateDTO;
import com.xxfc.platform.universal.dto.UploadImgDTO;
import com.xxfc.platform.universal.feign.MQSenderFeign; import com.xxfc.platform.universal.feign.MQSenderFeign;
import com.xxfc.platform.universal.feign.RegionFeign; import com.xxfc.platform.universal.feign.RegionFeign;
import com.xxfc.platform.universal.feign.ThirdFeign; import com.xxfc.platform.universal.feign.ThirdFeign;
...@@ -366,6 +367,7 @@ public class AppPermissionService { ...@@ -366,6 +367,7 @@ public class AppPermissionService {
log.info("register------username====="+username+"----channel===="+channel); log.info("register------username====="+username+"----channel===="+channel);
rsUserDetail.setChannel(channel); rsUserDetail.setChannel(channel);
rsUserDetail.setRegisterSource(registerSource); rsUserDetail.setRegisterSource(registerSource);
setHeadImg(rsUserDetail);
appUserDetailBiz.insertSelective(rsUserDetail); appUserDetailBiz.insertSelective(rsUserDetail);
log.info("注册:新增用户详情: " + userid+"---time===="+System.currentTimeMillis()/1000L); log.info("注册:新增用户详情: " + userid+"---time===="+System.currentTimeMillis()/1000L);
/* //绑定上下线关系 /* //绑定上下线关系
...@@ -425,6 +427,21 @@ public class AppPermissionService { ...@@ -425,6 +427,21 @@ public class AppPermissionService {
} }
} }
public void setHeadImg(AppUserDetail appUserDetail){
UploadImgDTO uploadImgDTO = new UploadImgDTO();
uploadImgDTO.setUserId(appUserDetail.getUserid());
uploadImgDTO.setPrefix(appUserDetail.getHeadimgurl());
try {
ObjectRestResponse<String> restResponse = thirdFeign.uploadHead(uploadImgDTO);
if (StringUtils.isNotBlank(restResponse.getData())){
appUserDetail.setHeadimgurl(restResponse.getData());
}
imFeign.
}catch (Exception e){
e.printStackTrace();
}
}
public void test(){ public void test(){
Example example=new Example(AppUserLogin.class); Example example=new Example(AppUserLogin.class);
...@@ -651,6 +668,7 @@ public class AppPermissionService { ...@@ -651,6 +668,7 @@ public class AppPermissionService {
userDetail.setChannel(channel); userDetail.setChannel(channel);
userDetail.setRegisterSource(registerSource); userDetail.setRegisterSource(registerSource);
//setCreateIPInfo(userDetail); //setCreateIPInfo(userDetail);
setHeadImg(userDetail);
appUserDetailBiz.insertSelective(userDetail); appUserDetailBiz.insertSelective(userDetail);
} /*else { } /*else {
......
...@@ -20,6 +20,9 @@ import java.util.List; ...@@ -20,6 +20,9 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public class UploadImgDTO { public class UploadImgDTO {
@ApiModelProperty("userId")
private Integer userId;
@ApiModelProperty("路径前缀") @ApiModelProperty("路径前缀")
private String prefix; private String prefix;
@ApiModelProperty("图片") @ApiModelProperty("图片")
......
...@@ -3,6 +3,7 @@ package com.xxfc.platform.universal.feign; ...@@ -3,6 +3,7 @@ package com.xxfc.platform.universal.feign;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse; import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.universal.dto.SmsTemplateDTO; import com.xxfc.platform.universal.dto.SmsTemplateDTO;
import com.xxfc.platform.universal.dto.UploadImgDTO;
import com.xxfc.platform.universal.dto.VehicleViolateDto; import com.xxfc.platform.universal.dto.VehicleViolateDto;
import com.xxfc.platform.universal.entity.Dictionary; import com.xxfc.platform.universal.entity.Dictionary;
import com.xxfc.platform.universal.entity.IdInformation; import com.xxfc.platform.universal.entity.IdInformation;
...@@ -104,4 +105,7 @@ public interface ThirdFeign { ...@@ -104,4 +105,7 @@ public interface ThirdFeign {
/************************************发票*************************************/ /************************************发票*************************************/
@PostMapping("/invoice/invoicing") @PostMapping("/invoice/invoicing")
public ObjectRestResponse invoicing(@RequestBody InvoiceVo invoiceVo); public ObjectRestResponse invoicing(@RequestBody InvoiceVo invoiceVo);
@RequestMapping(value = "app/unauth/uploadHead", method = RequestMethod.POST)
ObjectRestResponse<String> uploadHead(@RequestBody UploadImgDTO uploadImgDTO)throws Exception;
} }
package com.xxfc.platform.universal.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.admin.entity.AppUserDetail;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.result.JsonResultUtil;;
import com.xxfc.platform.universal.dto.UploadImgDTO;
import com.xxfc.platform.universal.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* 图片上传
*/
@RestController
@RequestMapping("app/file")
@IgnoreUserToken
@Slf4j
public class AppUploadController {
@Autowired
UploadService uploadService;
private static Integer MAX_DRIVING_LICENSE_SIZE = 10 * 1024 * 1024;//10M
@RequestMapping(value = "uploadV2", method = RequestMethod.POST)
public JSONObject uploadV2(@RequestParam("file") MultipartFile file)throws Exception {
if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
return JsonResultUtil.createFailedResult(2002,"大小超过限制!!!");
}
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFileV2(file, Long.parseLong(BaseContextHandler.getUserID())));
}
@RequestMapping(value = "app/unauth/uploadHead", method = RequestMethod.POST)
@IgnoreUserToken
public ObjectRestResponse<String> uploadHead(@RequestBody UploadImgDTO uploadImgDTO)throws Exception {
return ObjectRestResponse.succ(uploadService.getHeadImg(uploadImgDTO));
}
}
...@@ -3,8 +3,10 @@ package com.xxfc.platform.universal.service; ...@@ -3,8 +3,10 @@ package com.xxfc.platform.universal.service;
import com.github.wxiaoqi.security.common.util.process.SystemConfig; import com.github.wxiaoqi.security.common.util.process.SystemConfig;
import com.xxfc.platform.universal.constant.RedisKey; import com.xxfc.platform.universal.constant.RedisKey;
import com.xxfc.platform.universal.dto.UploadImgDTO;
import com.xxfc.platform.universal.utils.ImgBase64Util; import com.xxfc.platform.universal.utils.ImgBase64Util;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
...@@ -17,8 +19,9 @@ import org.springframework.http.MediaType; ...@@ -17,8 +19,9 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.io.File; import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -30,7 +33,8 @@ public class UploadService { ...@@ -30,7 +33,8 @@ public class UploadService {
private String baseUploadPath ; private String baseUploadPath ;
@Value("${universal.videoUploadPath}") @Value("${universal.videoUploadPath}")
private String videoUploadPath ; private String videoUploadPath ;
@Value("${universal.avatar}")
private String avatar ;
@Value("${universal.url}") @Value("${universal.url}")
private String xx_url ; private String xx_url ;
@Autowired @Autowired
...@@ -60,6 +64,18 @@ public class UploadService { ...@@ -60,6 +64,18 @@ public class UploadService {
return realFileRelPath; return realFileRelPath;
} }
public String uploadFileV2(MultipartFile file,Long userId) throws Exception{
//创建本日存放目录
String fileName = file.getOriginalFilename();
String realFileRelPath = avatar + "/" + userId % 1000 + "/" + userId + ".jpg";
//文件存放路径
String filePath = baseUploadPath + realFileRelPath ;
//将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath;
return realFileRelPath;
}
/** /**
* 写入上传文件,返回相对路径 * 写入上传文件,返回相对路径
* @param imgFile * @param imgFile
...@@ -170,5 +186,136 @@ public class UploadService { ...@@ -170,5 +186,136 @@ public class UploadService {
return realFileRelPath; return realFileRelPath;
} }
/**
*
* @param srcpic //原图片路径
* @param despic //目的图片路径
*/
public static void copyPic(String srcpic, String despic){
File file1 = new File(srcpic);
File file2 = new File(despic);
FileInputStream in=null;
try {
in=new FileInputStream(file1);
FileUtils.copyInputStreamToFile(in,file2 );
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String uploadHead(Integer userId) throws Exception{
//创建本日存放目录
String realFileRelPath = avatar + "/" + userId % 1000 + "/" + userId + ".jpg";
//文件存放路径
String filePath = baseUploadPath + realFileRelPath ;
copyPicV2(filePath);
realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath;
return realFileRelPath;
}
public String getHeadImg(UploadImgDTO uploadImgDTO) throws Exception{
String prefix = uploadImgDTO.getPrefix();
Integer userId = uploadImgDTO.getUserId();
String headImg;
if (StringUtils.isNotBlank(prefix)) {
headImg = getImg(prefix,userId);
}else {
headImg = uploadHead(userId);
}
return headImg;
}
/**
*
* @param despic //目的图片路径
*/
public void copyPicV2(String despic){
String srcpic = baseUploadPath+avatar+"default.png";
File file1 = new File(srcpic);
File file2 = new File(despic);
FileInputStream in=null;
try {
in=new FileInputStream(file1);
FileUtils.copyInputStreamToFile(in,file2 );
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getImg(String imgUrl,Integer userId) throws Exception {
//new一个URL对象
URL url = new URL(imgUrl);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
//创建本日存放目录
String filePath = avatar + userId % 1000;
File file=new File(baseUploadPath+filePath);
if(!file.exists()){//如果文件夹不存在
file.mkdir();//创建文件夹
}
String realFileRelPath= filePath + "/" + userId + ".jpg";
//文件存放路径
filePath = baseUploadPath + realFileRelPath ;
File imageFile = new File(filePath);
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流
outStream.close();
realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath;
return realFileRelPath;
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=inStream.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment