附件SDK增加了通过 ByteArray 上传文件接口

This commit is contained in:
ajaxfan
2020-12-29 09:23:12 +08:00
parent 2d83008262
commit 0acb09c80d
3 changed files with 61 additions and 6 deletions

View File

@ -62,4 +62,13 @@ public interface AttachmentClient {
*/
Optional<UploadObject> upload(Long businessId, File file);
/**
* 上传资源文件
*
* @param bid
* @param file
* @return
*/
Optional<UploadObject> upload(Long businessId, String filename, byte[] array);
}

View File

@ -70,7 +70,9 @@ public class DefaultAttachmentClient implements AttachmentClient {
}
/**
* @param businessId
* 文件下载
*
* @param businessId 业务id
* @return
*/
@Override
@ -79,7 +81,9 @@ public class DefaultAttachmentClient implements AttachmentClient {
}
/**
* @param objectId
* 文件下载
*
* @param objectId 对象id
* @return
*/
@Override
@ -88,8 +92,10 @@ public class DefaultAttachmentClient implements AttachmentClient {
}
/**
* @param businessId
* @param file
* 上传附件
*
* @param businessId 业务id
* @param file 文件
* @return
*/
@Override
@ -102,4 +108,23 @@ public class DefaultAttachmentClient implements AttachmentClient {
return Optional.ofNullable(new UploadObject().setId(feedback.getOid()));
}
/**
* 上传附件
*
* @param businessId 业务ID
* @param filename 文件名称
* @param array 文件字节组
* @return
*/
@Override
public Optional<UploadObject> upload(Long businessId, String filename, byte[] array) {
Objects.requireNonNull(businessId);
Objects.requireNonNull(array);
FeedbackMessage feedback = queryService.handleFileUpload(businessId,
FileConvertor.toMultipartFile(filename, array));
return Optional.ofNullable(new UploadObject().setId(feedback.getOid()));
}
}

View File

@ -13,19 +13,21 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
/**
* 文件转换器
*
* @author Ajaxfan
*/
public final class FileConvertor {
/**
* 将 File 对象转换为 MultipartFile 对象
*
* @param file 原始文件对象
* @return
*/
public static MultipartFile toMultipartFile(File file) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem item = factory.createItem("file", "text/plain", true, file.getName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
@ -41,4 +43,23 @@ public final class FileConvertor {
return new CommonsMultipartFile(item);
}
/**
* 文件字节数组转换为上传资源
*
* @param filename 文件名称
* @param array 文件字节组
* @return
*/
public static MultipartFile toMultipartFile(String filename, byte[] array) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem item = factory.createItem("file", "text/plain", true, filename);
try (OutputStream os = item.getOutputStream()) {
os.write(array);
} catch (IOException e) {
e.printStackTrace();
}
return new CommonsMultipartFile(item);
}
}