|
@@ -0,0 +1,193 @@
|
|
|
+package cn.efunbox.audio.controller;
|
|
|
+
|
|
|
+import cn.efunbox.audio.utils.ApiCode;
|
|
|
+import cn.efunbox.audio.utils.HttpUtil;
|
|
|
+import cn.efunbox.audio.utils.SnowflakeIdUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * FileUploadController
|
|
|
+ * Created by xusq on 2018/3/12.
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/file")
|
|
|
+public class FileUploadController {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * ossbuckt名
|
|
|
+ */
|
|
|
+ public static String BUCKETNAME = "efunimgs";
|
|
|
+
|
|
|
+ // 阿里云API的内或外网域名
|
|
|
+ @Value("${ali.oss.endpoint}")
|
|
|
+ private String endpoint;
|
|
|
+ // 阿里云API的密钥Access Key ID
|
|
|
+ @Value("${ali.oss.accessKeyId}")
|
|
|
+ private String accessKeyId;
|
|
|
+ // 阿里云API的密钥Access Key Secret
|
|
|
+ @Value("${ali.oss.accessKeySecret}")
|
|
|
+ private String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.file.prefix}")
|
|
|
+ private String ossPrefix;
|
|
|
+
|
|
|
+ @Value("${efunbox.oss.img.url}")
|
|
|
+ private String imgURL;
|
|
|
+
|
|
|
+ private static final String DOT = ".";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping(value="/uploads", method = RequestMethod.POST)
|
|
|
+ public void uploadFiles(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
+ Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
+ Set<String> fileNames = fileMap.keySet() ;
|
|
|
+ try {
|
|
|
+ // key-图片名称 value 地址
|
|
|
+ Map<String, String> map = new HashMap<String, String>();
|
|
|
+ for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
+ MultipartFile mf = entity.getValue();
|
|
|
+ String fileName = mf.getOriginalFilename();
|
|
|
+ fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
|
|
|
+ long size = mf.getSize() ;
|
|
|
+ if( size > MAX_LENGTH ){
|
|
|
+ HttpUtil.responseApiCode(request, response, ApiCode.FILE_TO_BIG);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ String suffix = fileName.lastIndexOf(DOT) != -1 ? fileName.substring(fileName.lastIndexOf(DOT)) : ".jpg";
|
|
|
+ String ossName = upload(mf,suffix, size);
|
|
|
+ if (ossName == null) {// 上传失败
|
|
|
+ HttpUtil.responseApiCode(request, response, ApiCode.FILE_UPLOAD_ERROR);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ map.put(fileName, ossName);
|
|
|
+ }
|
|
|
+ HttpUtil.responseOkData(request, response,map);
|
|
|
+ return;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("uploadFiles failed! files={}", JSON.toJSONString(fileNames), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/upload", method = RequestMethod.POST)
|
|
|
+ public void uploadFile(HttpServletRequest request, HttpServletResponse response,String code) throws Exception {
|
|
|
+ if (request.getCharacterEncoding() == null) {
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+ }
|
|
|
+ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
+ Iterator<String> iterator = multipartRequest.getFileNames();
|
|
|
+ MultipartFile multipartFile = multipartRequest.getFile(iterator.next());
|
|
|
+ String fileName=multipartFile.getOriginalFilename();
|
|
|
+ String suffix = null;
|
|
|
+ if(!StringUtils.isEmpty(fileName)){
|
|
|
+ //解决中文乱码
|
|
|
+ suffix = fileName.lastIndexOf(DOT) != -1 ? fileName.substring(fileName.lastIndexOf(DOT)) : ".jpg";
|
|
|
+ }
|
|
|
+ long size = multipartFile.getSize() ;
|
|
|
+ if( size > MAX_LENGTH ){
|
|
|
+ HttpUtil.responseApiCode(request, response, ApiCode.FILE_TO_BIG);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+// if(StringUtils.isNotBlank(code)){
|
|
|
+// //截取后缀名 根据code从新命名
|
|
|
+
|
|
|
+// fileName = code+suffix;
|
|
|
+// }
|
|
|
+
|
|
|
+// String ossName = upload(fileName, multipartFile, size);
|
|
|
+ String ossName = upload(multipartFile,suffix, size);
|
|
|
+ if (ossName == null) {// 上传失败
|
|
|
+ HttpUtil.responseApiCode(request, response, ApiCode.FILE_UPLOAD_ERROR);
|
|
|
+ }
|
|
|
+ HttpUtil.responseOkData(request, response,"/"+ossName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static final int MAX_LENGTH = 10 * 1024 * 1024 ;
|
|
|
+
|
|
|
+ private String upload(MultipartFile multipartFile,String suffix, long size){
|
|
|
+ log.info("UUID= {} filename={} size= {}",UUID.randomUUID(),size);
|
|
|
+ String pathName = null;
|
|
|
+ try {
|
|
|
+ if (size > MAX_LENGTH) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ pathName = save2Oss(multipartFile,suffix);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("upload filename={} size={}", size, e);
|
|
|
+ }
|
|
|
+ if (pathName == null) {// 上传失败
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("UUID= {} filename={} size= {} 上传 result= {} ",UUID.randomUUID(),size,pathName);
|
|
|
+ return pathName;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String save2Oss(MultipartFile file,String suffix) throws IOException {
|
|
|
+
|
|
|
+ String code = SnowflakeIdUtil.getSnowflakeIdUtil().nextCode();
|
|
|
+ String savePath = ossPrefix + getDateStr() + "/"+ code + suffix;
|
|
|
+ InputStream is = file.getInputStream();
|
|
|
+ Long fileSize = file.getSize();
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ metadata.setContentLength(is.available());
|
|
|
+ metadata.setCacheControl("no-cache");
|
|
|
+ metadata.setHeader("Pragma", "no-cache");
|
|
|
+ metadata.setContentEncoding("utf-8");
|
|
|
+// metadata.setContentType(getContentType(fileName));
|
|
|
+ metadata.setContentDisposition("filename/filesize=" + savePath + "/" + fileSize + "Byte.");
|
|
|
+ OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ ossClient.putObject(BUCKETNAME, savePath, is, metadata);
|
|
|
+ return savePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成随机文件名:当前年月日时分秒+五位随机数
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getDateStr() {
|
|
|
+
|
|
|
+ SimpleDateFormat simpleDateFormat;
|
|
|
+
|
|
|
+ simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
+
|
|
|
+ Date date = new Date();
|
|
|
+
|
|
|
+ String str = simpleDateFormat.format(date);
|
|
|
+
|
|
|
+ /* Random random = new Random();
|
|
|
+
|
|
|
+ int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数*/
|
|
|
+
|
|
|
+ return str;// 当前时间
|
|
|
+ }
|
|
|
+
|
|
|
+}
|