base64上传
This commit is contained in:
parent
e07c9419b0
commit
bf7f5dc8dc
|
|
@ -58,6 +58,7 @@ public interface RemoteFileService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回字节
|
* 返回字节
|
||||||
|
*
|
||||||
* @param fileId
|
* @param fileId
|
||||||
* @param source
|
* @param source
|
||||||
* @return
|
* @return
|
||||||
|
|
@ -76,8 +77,8 @@ public interface RemoteFileService {
|
||||||
* @author cwchen
|
* @author cwchen
|
||||||
* @date 2024/3/21 17:25
|
* @date 2024/3/21 17:25
|
||||||
*/
|
*/
|
||||||
@PostMapping(value = "/file/singleUploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/file/singleUploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
public Result singleUploadFile(@RequestPart(value = "file") MultipartFile file,@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
public Result singleUploadFile(@RequestPart(value = "file") MultipartFile file, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多个文件上传
|
* 多个文件上传
|
||||||
|
|
@ -89,11 +90,12 @@ public interface RemoteFileService {
|
||||||
* @author cwchen
|
* @author cwchen
|
||||||
* @date 2024/3/21 17:25
|
* @date 2024/3/21 17:25
|
||||||
*/
|
*/
|
||||||
@PostMapping(value = "/file/mostUploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/file/mostUploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
public Result mostUploadFile(@RequestPart(value = "files") MultipartFile[] files,@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
public Result mostUploadFile(@RequestPart(value = "files") MultipartFile[] files, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
|
*
|
||||||
* @param fileId
|
* @param fileId
|
||||||
* @param source
|
* @param source
|
||||||
* @return AjaxResult
|
* @return AjaxResult
|
||||||
|
|
@ -102,5 +104,16 @@ public interface RemoteFileService {
|
||||||
* @date 2024/3/21 17:27
|
* @date 2024/3/21 17:27
|
||||||
*/
|
*/
|
||||||
@PostMapping(value = "/file/delFile")
|
@PostMapping(value = "/file/delFile")
|
||||||
public Result delFile(@RequestBody String fileId,@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
public Result delFile(@RequestBody String fileId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* base64文件上传
|
||||||
|
* @param base64
|
||||||
|
* @param source
|
||||||
|
* @return Result
|
||||||
|
* @author cwchen
|
||||||
|
* @date 2024/6/11 10:22
|
||||||
|
*/
|
||||||
|
@PostMapping(value = "/file/uploadFileByBase64", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
|
public Result uploadFileByBase64(@RequestPart(value = "base64") String base64, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,11 @@ public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileServ
|
||||||
public Result delFile(String fileId, String source) {
|
public Result delFile(String fileId, String source) {
|
||||||
return Result.fail("文件服务调用失败---删除文件:" + throwable.getMessage());
|
return Result.fail("文件服务调用失败---删除文件:" + throwable.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result uploadFileByBase64(String base64, String source) {
|
||||||
|
return Result.fail("上传base64文件失败---:" + throwable.getMessage());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
package com.securitycontrol.common.core.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.codec.Base64;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import sun.misc.BASE64Decoder;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.*;
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-06-11 17:53-10:10
|
||||||
|
* @version:1.0
|
||||||
|
* @description:字节数组转文件
|
||||||
|
*/
|
||||||
|
public class BASE64DecodedMultipartFile implements MultipartFile {
|
||||||
|
|
||||||
|
private final byte[] imgContent;
|
||||||
|
private final String header;
|
||||||
|
|
||||||
|
public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
|
||||||
|
this.imgContent = imgContent;
|
||||||
|
this.header = header.split(";")[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOriginalFilename() {
|
||||||
|
return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContentType() {
|
||||||
|
return header.split(":")[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return imgContent == null || imgContent.length == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getSize() {
|
||||||
|
return imgContent.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBytes() throws IOException {
|
||||||
|
return imgContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getInputStream() throws IOException {
|
||||||
|
return new ByteArrayInputStream(imgContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void transferTo(File dest) throws IOException, IllegalStateException {
|
||||||
|
new FileOutputStream(dest).write(imgContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MultipartFile base64ToMultipart(String base64) {
|
||||||
|
try {
|
||||||
|
String[] baseStrs = base64.split(",");
|
||||||
|
|
||||||
|
BASE64Decoder decoder = new BASE64Decoder();
|
||||||
|
byte[] b = new byte[0];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getBase64(String path) {
|
||||||
|
File file = new File(path);
|
||||||
|
String base64 = null;
|
||||||
|
try {
|
||||||
|
BufferedImage image = ImageIO.read(file);
|
||||||
|
Integer width = image.getWidth();
|
||||||
|
Integer height = image.getHeight();
|
||||||
|
System.out.println("宽:" + width + " 高:" + height);
|
||||||
|
|
||||||
|
//输出流
|
||||||
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(image, "png", stream);
|
||||||
|
base64 = Base64.encode(stream.toByteArray());
|
||||||
|
System.out.println(base64);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return base64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.securitycontrol.files.controller;
|
package com.securitycontrol.files.controller;
|
||||||
|
|
||||||
import com.securitycontrol.common.core.domain.Result;
|
import com.securitycontrol.common.core.domain.Result;
|
||||||
|
import com.securitycontrol.common.core.utils.BASE64DecodedMultipartFile;
|
||||||
import com.securitycontrol.entity.file.FileExportVo;
|
import com.securitycontrol.entity.file.FileExportVo;
|
||||||
import com.securitycontrol.files.mongodb.service.FileUploadService;
|
import com.securitycontrol.files.mongodb.service.FileUploadService;
|
||||||
import com.securitycontrol.files.mongodb.util.MongodbFileUtil;
|
import com.securitycontrol.files.mongodb.util.MongodbFileUtil;
|
||||||
|
|
@ -110,4 +111,17 @@ public class FileUploadController {
|
||||||
return Result.fail();
|
return Result.fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "base64上传")
|
||||||
|
@PostMapping("uploadFileByBase64")
|
||||||
|
public Result uploadFileByBase64(@RequestParam(value = "base64", required = false) String base64){
|
||||||
|
try {
|
||||||
|
MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64);
|
||||||
|
FileExportVo fileExportVo = mongoService.uploadFile(multipartFile);
|
||||||
|
return Result.ok(fileExportVo);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("base64上传",e);
|
||||||
|
return Result.fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue