123
This commit is contained in:
1 parent
1957b0a1ed
commit
f084d04808
5 files changed
+89
-1
No files matched your search
@@ -0,0 +1,61 @@
|
|||||||
|
package com.ruoyi.common.utils;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by yihui on 2017/7/17.
|
||||||
|
*/
|
||||||
|
public class Base64Util {
|
||||||
|
|
||||||
|
public static String encode(BufferedImage bufferedImage, String imgType) throws IOException {
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(bufferedImage, imgType, outputStream);
|
||||||
|
return encode(outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String imageToBase64(String imagePath) {
|
||||||
|
try {
|
||||||
|
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
|
||||||
|
return Base64.getEncoder().encodeToString(imageBytes);
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static String encode(ByteArrayOutputStream outputStream) {
|
||||||
|
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* base64转图片并保存到本地
|
||||||
|
*
|
||||||
|
* @param base64 base64字符串
|
||||||
|
* @param imagePath 保存路径,例如 D:/test/output.png
|
||||||
|
*/
|
||||||
|
public static void base64ToImage(String base64, String imagePath) {
|
||||||
|
// 去掉 data:image/png;base64, 这种前缀
|
||||||
|
try {
|
||||||
|
if (base64.contains(",")) {
|
||||||
|
base64 = base64.substring(base64.indexOf(",") + 1);
|
||||||
|
}
|
||||||
|
byte[] imageBytes = Base64.getDecoder().decode(base64);
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(imagePath)) {
|
||||||
|
fos.write(imageBytes);
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static BufferedImage decode2Img(String base64) throws IOException {
|
||||||
|
byte[] bytes = Base64.getDecoder().decode(base64.getBytes("utf-8"));
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
||||||
|
return ImageIO.read(inputStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.ruoyi.system.controller;
|
package com.ruoyi.system.controller;
|
||||||
|
|
||||||
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
|
import com.ruoyi.system.domain.Base64UploadVo;
|
||||||
import com.ruoyi.system.service.IFileUploadService;
|
import com.ruoyi.system.service.IFileUploadService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -28,7 +29,11 @@ public class FileUploadController {
|
|||||||
return AjaxResult.error(e.getMessage());
|
return AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@PostMapping("/upload-base64")
|
||||||
|
public AjaxResult uploadBase64File(@RequestBody Base64UploadVo vo){
|
||||||
|
AjaxResult ajax = AjaxResult.success(fileUploadService.uploadBase64File(vo));
|
||||||
|
return ajax;
|
||||||
|
}
|
||||||
@PostMapping("/uploads")
|
@PostMapping("/uploads")
|
||||||
public AjaxResult uploadFiles(@RequestParam("files") List<MultipartFile> files,
|
public AjaxResult uploadFiles(@RequestParam("files") List<MultipartFile> files,
|
||||||
@RequestParam(value = "module", required = false, defaultValue = "common") String module) {
|
@RequestParam(value = "module", required = false, defaultValue = "common") String module) {
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.ruoyi.system.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Base64UploadVo {
|
||||||
|
public String base64Data;
|
||||||
|
private String fileName;
|
||||||
|
private String path;
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.ruoyi.system.service;
|
package com.ruoyi.system.service;
|
||||||
|
|
||||||
|
import com.ruoyi.system.domain.Base64UploadVo;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -13,4 +14,6 @@ public interface IFileUploadService {
|
|||||||
boolean deleteFile(String filePath);
|
boolean deleteFile(String filePath);
|
||||||
|
|
||||||
String getFileUrl(String filePath);
|
String getFileUrl(String filePath);
|
||||||
|
|
||||||
|
boolean uploadBase64File(Base64UploadVo vo);
|
||||||
}
|
}
|
||||||
@@ -5,10 +5,12 @@ import com.ruoyi.common.constant.Constants;
|
|||||||
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
||||||
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
|
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
|
||||||
import com.ruoyi.common.exception.file.InvalidExtensionException;
|
import com.ruoyi.common.exception.file.InvalidExtensionException;
|
||||||
|
import com.ruoyi.common.utils.Base64Util;
|
||||||
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.common.utils.file.MimeTypeUtils;
|
import com.ruoyi.common.utils.file.MimeTypeUtils;
|
||||||
import com.ruoyi.common.utils.uuid.IdUtils;
|
import com.ruoyi.common.utils.uuid.IdUtils;
|
||||||
|
import com.ruoyi.system.domain.Base64UploadVo;
|
||||||
import com.ruoyi.system.service.IFileUploadService;
|
import com.ruoyi.system.service.IFileUploadService;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -89,6 +91,13 @@ public class FileUploadServiceImpl implements IFileUploadService {
|
|||||||
return Constants.RESOURCE_PREFIX + "/" + filePath;
|
return Constants.RESOURCE_PREFIX + "/" + filePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean uploadBase64File(Base64UploadVo vo) {
|
||||||
|
String path = vo.getPath() + vo.getFileName();
|
||||||
|
Base64Util.base64ToImage(vo.getBase64Data(), path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private String getUploadPath(String module) {
|
private String getUploadPath(String module) {
|
||||||
String basePath = RuoYiConfig.getUploadPath();
|
String basePath = RuoYiConfig.getUploadPath();
|
||||||
if (StringUtils.isNotEmpty(module)) {
|
if (StringUtils.isNotEmpty(module)) {
|
||||||
|
|||||||
Reference in new issue
Block a user