|
@@ -6,10 +6,7 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.MediaType;
|
|
|
-import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
@@ -18,6 +15,9 @@ import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.file.FileUploadUtils;
|
|
|
import com.ruoyi.common.utils.file.FileUtils;
|
|
|
import com.ruoyi.framework.config.ServerConfig;
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* 通用请求处理
|
|
@@ -89,6 +89,31 @@ public class CommonController
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 通用上传请求
|
|
|
+ */
|
|
|
+ @PostMapping("/common/uploadBase")
|
|
|
+ public AjaxResult uploadFileBase64(@RequestBody String file) throws Exception
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ MultipartFile multipartFile = base64ToMultipart(file);
|
|
|
+ // 上传文件路径
|
|
|
+ String filePath = RuoYiConfig.getUploadPath();
|
|
|
+ // 上传并返回新文件名称
|
|
|
+ String fileName = FileUploadUtils.upload(filePath, multipartFile);
|
|
|
+ String url = serverConfig.getUrl() + fileName;
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ ajax.put("fileName", fileName);
|
|
|
+ ajax.put("url", url);
|
|
|
+ return ajax;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ return AjaxResult.error(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 本地资源通用下载
|
|
|
*/
|
|
@@ -117,4 +142,27 @@ public class CommonController
|
|
|
log.error("下载文件失败", e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * base64转MultipartFile
|
|
|
+ * @param base64
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static MultipartFile base64ToMultipart(String base64) {
|
|
|
+ try {
|
|
|
+ String[] baseStrs = base64.split(",");
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
+ byte[] b;
|
|
|
+ b = decoder.decodeBuffer(baseStrs[1]);
|
|
|
+ for(int i = 0; i < b.length; ++i) {
|
|
|
+ if (b[i] < 0) {
|
|
|
+ b[i] += 256;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new Base64DecodedMultipartFile(b, baseStrs[0]);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|