@ -1,6 +1,8 @@
package com.ccsens.mt.api ;
package com.ccsens.mt.api ;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.lang.Snowflake ;
import cn.hutool.core.util.ObjectUtil ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.alibaba.fastjson.JSONObject ;
import com.ccsens.cloudutil.feign.TallFeignClient ;
import com.ccsens.cloudutil.feign.TallFeignClient ;
@ -20,6 +22,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.Part ;
import javax.servlet.http.Part ;
import java.io.File ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.List ;
@ -38,6 +41,8 @@ public class FileController {
private IFileService fileService ;
private IFileService fileService ;
@Resource
@Resource
private TallFeignClient tallFeignClient ;
private TallFeignClient tallFeignClient ;
@Resource
private Snowflake snowflake ;
@ApiOperation ( value = "上传文件" )
@ApiOperation ( value = "上传文件" )
@ -88,12 +93,8 @@ public class FileController {
@ApiImplicitParams ( {
@ApiImplicitParams ( {
} )
} )
@RequestMapping ( value = "/upload/photo" , method = RequestMethod . POST , produces = { "application/json;charset=UTF-8" } )
@RequestMapping ( value = "/upload/photo" , method = RequestMethod . POST , produces = { "application/json;charset=UTF-8" } )
public JsonResponse < FileVo . Upload > upload ( HttpServletRequest request , @RequestParam ( ) Part file ) throws Exception {
public JsonResponse < FileVo . Upload > uploadPhone ( HttpServletRequest request , @RequestParam ( ) Part file ) throws Exception {
log . info ( "文件上传:{}" , file ) ;
log . info ( "文件上传:{}" , file ) ;
//限制文件大小 2M
if ( file . getSize ( ) > 2097152 ) {
throw new BaseException ( CodeEnum . PHOTO_FILE_EXCEED_2M ) ;
}
String authHeader = request . getHeader ( WebConstant . HEADER_KEY_TOKEN ) ;
String authHeader = request . getHeader ( WebConstant . HEADER_KEY_TOKEN ) ;
JsonResponse tokenRes = tallFeignClient . getUserIdByToken ( authHeader ) ;
JsonResponse tokenRes = tallFeignClient . getUserIdByToken ( authHeader ) ;
log . info ( "{}查询userId返回:{}" , authHeader , tokenRes ) ;
log . info ( "{}查询userId返回:{}" , authHeader , tokenRes ) ;
@ -102,16 +103,50 @@ public class FileController {
}
}
JSONObject json = JSON . parseObject ( JSON . toJSONString ( tokenRes . getData ( ) ) ) ;
JSONObject json = JSON . parseObject ( JSON . toJSONString ( tokenRes . getData ( ) ) ) ;
Long userId = json . getLong ( "id" ) ;
Long userId = json . getLong ( "id" ) ;
String dir = PropUtil . path + Constant . File . UPLOAD_URL ;
FileVo . Upload vo = fileService . uploadPhone ( file , userId ) ;
List < Part > files = new ArrayList < > ( ) ;
// if(ObjectUtil.isNotNull(file)) {
files . add ( file ) ;
// //限制文件大小 2M
List < CommonFile > fileVos = fileService . saveFile ( dir , files , userId ) ;
// if (file.getSize() > 2097152) {
List < FileVo . Upload > vos = FileVo . Upload . transFilePo ( fileVos ) ;
// throw new BaseException(CodeEnum.PHOTO_FILE_EXCEED_2M);
log . info ( "文件上传返回:{}" , vos ) ;
// }
FileVo . Upload vo = new FileVo . Upload ( ) ;
// String authHeader = request.getHeader(WebConstant.HEADER_KEY_TOKEN);
if ( CollectionUtil . isNotEmpty ( vos ) ) {
// JsonResponse tokenRes = tallFeignClient.getUserIdByToken(authHeader);
vo = vos . get ( 0 ) ;
// log.info("{}查询userId返回:{}", authHeader, tokenRes);
}
// if (tokenRes.getCode().intValue() != CodeEnum.SUCCESS.getCode().intValue()) {
// return tokenRes;
// }
// JSONObject json = JSON.parseObject(JSON.toJSONString(tokenRes.getData()));
// Long userId = json.getLong("id");
// String dir = PropUtil.path + Constant.File.UPLOAD_URL;
//// List<Part> files = new ArrayList<>();
//// files.add(file);
// String allowedExt = WebConstant.IMG_TYPE + "," + WebConstant.Wps.FILE_TYPE_ALL;
//
// log.info("文件名:{}", file.getSubmittedFileName());
// String path = UploadFileUtil_Servlet3.uploadFile(file, allowedExt, dir);
// CommonFile fileDo = new CommonFile();
// String name = file.getSubmittedFileName();
// fileDo.setId(snowflake.nextId());
// fileDo.setFileName(name);
// fileDo.setLocation(dir + java.io.File.separator + path);
// fileDo.setUserId(userId);
// if (WebConstant.IMG_TYPE.contains(name.substring(name.lastIndexOf(".") + 1))) {
// fileDo.setVisitLocation(PropUtil.imgDomain + Constant.File.UPLOAD_URL + File.separator + path);
// } else {
// fileDo.setVisitLocation(PropUtil.domain + "file/download/"+fileDo.getId());
// }
// commonFileMapper.insertSelective(fileDo);
// log.info("保存文件:{}", fileDo);
// fileList.add(fileDo);
//
//// List<CommonFile> fileVos = fileService.saveFile(dir, files, userId);
//// List<FileVo.Upload> vos = FileVo.Upload.transFilePo(fileVos);
//// log.info("文件上传返回:{}", vos);
//
// if (CollectionUtil.isNotEmpty(vos)) {
// vo = vos.get(0);
// }
// }
return JsonResponse . newInstance ( ) . ok ( vo ) ;
return JsonResponse . newInstance ( ) . ok ( vo ) ;
}
}
}
}