- 定义一个base64图片:
StringBuffer str = new StringBuffer();
String base64Img = "自行baidu搜索转换成字符";
str.append(base64Img);
/**
* base64转MultipartFile文件
* @param base64
* @return
*/
public static MultipartFile base64ToMultipart(String base64) {
try {
String baseStrs = base64.replaceFirst("data:image/jpeg;base64,", "");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
b = decoder.decodeBuffer(baseStrs);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
return new BASE64DecodedMultipartFile(b, baseStrs);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
- 定义四个MultipartFile文件,两个MultipartFile[] 数组,每个数组放2个
MultipartFile file1 = base64ToMultipart(str.toString());
MultipartFile file2 = base64ToMultipart(str.toString());
MultipartFile file3 = base64ToMultipart(str.toString());
MultipartFile file4 = base64ToMultipart(str.toString());
MultipartFile[] photoApproveMaterials1 = new MultipartFile[2];
MultipartFile[] photoApproveMaterials2 = new MultipartFile[2];
photoApproveMaterials1[0] = file1;
photoApproveMaterials1[1] = file2;
photoApproveMaterials2[0] = file3;
photoApproveMaterials2[1] = file4;
- 合到一个MultipartFile数组中
Integer multLength = photoApproveMaterials1.length + photoApproveMaterials2.length;
MultipartFile[] multipartFiles = new MultipartFile[multLength];
for( int i = 0;i <= multLength;i++){
for( int a = 0; a <= photoApproveMaterials1.length -1; a++){
multipartFiles[i] = photoApproveMaterials1[a];
i++;
System.out.println(i);
}
for( int b = 0; b <= photoApproveMaterials2.length -1; b++){
multipartFiles[i] = photoApproveMaterials2[b];
i++;
}
}
for( int i = 0;i <= multLength -1;i++){
MultipartFile mult = multipartFiles[i];
System.out.println(mult.getName());
System.out.println(mult.getSize());
}
System.out.println(multipartFiles.length);
- 如果大家有更好的方法,可以写在评论中