package com.sinata.modular.system.controller.util;
|
|
import com.sinata.config.properties.GunsProperties;
|
import com.sinata.core.base.controller.BaseController;
|
import com.sinata.core.common.exception.BizExceptionEnum;
|
import com.sinata.core.exception.GunsException;
|
import com.sinata.core.util.huawei.obs.ObsUploadUtil;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.File;
|
import java.util.UUID;
|
|
/**
|
* 上传文件
|
*/
|
@RestController
|
@RequestMapping("/upload")
|
public class UploadUtil extends BaseController {
|
private final static Logger log = LoggerFactory.getLogger(UploadUtil.class);
|
|
@Autowired
|
private GunsProperties gunsProperties;
|
|
/**
|
* 上传文件(采用上传本地服务器,或OSS服务器)
|
* @param picture 文件
|
* @param uploadWay 上传方式:1=上传OSS服务器
|
* @return
|
*/
|
private String uploadOssOrLocal(MultipartFile picture, Integer... uploadWay){
|
String pictureName = null;
|
try {
|
// 默认上传图片文件方式为本地上传
|
if(uploadWay == null || uploadWay.length == 0 || uploadWay[0] == 0) {
|
//获取文件名后缀
|
String suffix = picture.getOriginalFilename().substring(picture.getOriginalFilename().lastIndexOf("."));
|
pictureName = UUID.randomUUID().toString() + suffix;
|
|
// 文件目录路径
|
String fileSavePath = gunsProperties.getFileUploadPath();
|
File file = new File(fileSavePath + pictureName);
|
picture.transferTo(file);
|
} else {
|
pictureName = ObsUploadUtil.obsUpload(picture, "img");
|
}
|
} catch (Exception e) {
|
log.error(e.getMessage(), e);
|
throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
|
}
|
return pictureName;
|
}
|
|
/**
|
* 上传图片(上传到项目的webapp/static/img)
|
*/
|
@RequestMapping("/image")
|
public String image(@RequestPart("file") MultipartFile picture) {
|
// 上传文件(采用上传本地服务器,或OSS服务器)
|
return uploadOssOrLocal(picture ,gunsProperties.getFileUploadWay());
|
}
|
|
/**
|
* 上传文件
|
*/
|
@RequestMapping("/file")
|
public String file(@RequestPart("file") MultipartFile picture) {
|
// 上传文件(采用上传本地服务器,或OSS服务器)
|
return uploadOssOrLocal(picture ,gunsProperties.getFileUploadWay());
|
}
|
|
/**
|
* UEditor编辑器上传图片
|
*/
|
@RequestMapping("/imageUE")
|
public String imageUp(@RequestPart("upfile") MultipartFile picture, HttpServletRequest request) {
|
String callback = request.getParameter("callback");
|
String pictureName = null;
|
try {
|
// 上传文件(采用上传本地服务器,或OSS服务器)
|
pictureName = uploadOssOrLocal(picture ,gunsProperties.getFileUploadWay());
|
|
String result = "{'original': '" + picture.getOriginalFilename() + "', 'state': 'SUCCESS', 'url': '" + pictureName + "'}";
|
if (callback == null) {
|
return result;
|
} else {
|
return "<script>" + callback + "(" + result + ")</script>";
|
}
|
} catch (Exception e) {
|
log.error(e.getMessage(), e);
|
String result = "{'original': '', 'state': '文件上传失败','url': ''}";
|
if (callback == null) {
|
return result;
|
} else {
|
return "<script>" + callback + "(" + result + ")</script>";
|
}
|
}
|
}
|
|
}
|