Commit 16f97eb9 authored by zhuangzhuang's avatar zhuangzhuang

5.25--图片上传

parent 34c42b90
...@@ -14,7 +14,7 @@ public class OSSClientConstants { ...@@ -14,7 +14,7 @@ public class OSSClientConstants {
public static final String ACCESS_KEY_SECRET = "JgupdkkFg1Ot1eRsdk6wXU3CJiWdaw"; public static final String ACCESS_KEY_SECRET = "JgupdkkFg1Ot1eRsdk6wXU3CJiWdaw";
//阿里云API的bucket名称 //阿里云API的bucket名称
public static final String BACKET_NAME = "witcloud-oss";//uploadexcelhltp public static final String BACKET_NAME = "tdlcloud";//uploadexcelhltp
//阿里云API的文件夹名称 //阿里云API的文件夹名称
......
package com.example.tdl.web;
import com.aliyun.oss.OSSClient;
import com.example.tdl.domain.dto.CommFeedback;
import com.example.tdl.util.AliyunOSSClientUtil;
import com.google.gson.Gson;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController("/picUpload")
public class PicUploadController {
private final Logger LOGGER = LoggerFactory.getLogger(PicUploadController.class);
Gson gson = new Gson();
CommFeedback fb = new CommFeedback();
// 允许上传的格式
private static final String[] IMAGE_TYPE = new String[]{".jpg",".png" };
//上传图片
@ApiImplicitParams({
@ApiImplicitParam(paramType="header", name = "Account_token", value = "token", required = true, dataType = "String"),
})
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public String upload(MultipartFile multipartFile, @RequestParam(value = "imageType",required = true) String imageType) {
if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
fb.setCode(0);
fb.setMessage("图片不能为空");
return gson.toJson(fb);
}
if (multipartFile.getSize() > 5 * 1024 * 1024) {
fb.setCode(0);
fb.setMessage("上传图片大小不能超过5M!");
return gson.toJson(fb);
}
// 校验图片格式
boolean isLegal = false;
// 判断文件后缀名是否符合
for (String type : IMAGE_TYPE) {
if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(), type)) {
isLegal = true;
break;
}
}
if (isLegal) {
try {
//初始化OSSClient
OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
LOGGER.info(multipartFile.getOriginalFilename());
String url = AliyunOSSClientUtil.uploadImg2Oss(ossClient, multipartFile,System.currentTimeMillis()+"TM",imageType);
if (url == null) {
fb.setMessage("上传失败");
fb.setCode(0);
} else {
if("user".equals(imageType)){
//https://tdlcloud.oss-cn-shanghai.aliyuncs.com
fb.setMessage("https://tdlcloud.oss-cn-shanghai.aliyuncs.com/pic/userImage/" + url);
}else if("company".equals(imageType)){
fb.setMessage("https://tdlcloud.oss-cn-shanghai.aliyuncs.com/pic/companyLog/" + url);
}else if("palte".equals(imageType)){
fb.setMessage("https://tdlcloud.oss-cn-shanghai.aliyuncs.com/pic/palteImage/" + url);
}else if ("wareHouse".equals(imageType)){
fb.setMessage("https://tdlcloud.oss-cn-shanghai.aliyuncs.com/pic/wareHouseImage/" + url);
}else if("circuit".equals(imageType)){
fb.setMessage("https://tdlcloud.oss-cn-shanghai.aliyuncs.com/pic/userImage/" + url);
}
fb.setCode(1);
}
} catch (Exception e) {
fb.setCode(0);
fb.setMessage("上传失败");
e.printStackTrace();
LOGGER.info(e.toString());
}
} else {
fb.setCode(0);
fb.setMessage("上传图片格式不正确");
}
return gson.toJson(fb);
}
}
...@@ -252,17 +252,6 @@ public class WarehouseController { ...@@ -252,17 +252,6 @@ public class WarehouseController {
String datum = tokenRedisService.get("TOKEN_" +token); String datum = tokenRedisService.get("TOKEN_" +token);
UserRedisVo user = gson.fromJson(datum,UserRedisVo.class); UserRedisVo user = gson.fromJson(datum,UserRedisVo.class);
String warehouseNo = getWarehouseNo(); String warehouseNo = getWarehouseNo();
// if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
// fb.setCode(0);
// fb.setMessage("图片不能为空");
// return gson.toJson(fb);
// }
// if (multipartFile.getSize() > 5 * 1024 * 1024) {
// fb.setCode(0);
// fb.setMessage("上传图片大小不能超过5M!");
// return gson.toJson(fb);
// }
// String img = PicUploadUtil.uploadImage(multipartFile,warehouseNo,"warehouse");
if (StringUtils.isEmpty(addWarehouseVo.getWarehouseName())){ if (StringUtils.isEmpty(addWarehouseVo.getWarehouseName())){
fb.setCode(0); fb.setCode(0);
fb.setMessage("仓库名不能为空"); fb.setMessage("仓库名不能为空");
......
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