问题描述:
java.lang.IllegalArgumentException: Unexpected char 0x6d4b at 0 in Content-Type value: 测试文件.xlsx
问题分析:
1、使用new MockMultipartFile(String name, String originalFilename,String contentType, InputStream contentStream)方法生成MultipartFile对象时,contentType参数值不正确,导致MinIO上传文件报错。
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] barray = bos.toByteArray();
//byte[] to InputStream
InputStream is = new ByteArrayInputStream(barray);
//InputStream to MultipartFile
MultipartFile multipartFile = new MockMultipartFile("测试文件.xlsx", "测试文件.xlsx", "测试文件.xlsx", is);
} catch (Exception e) {
e.printStackTrace();
}
解决办法:
contentType参数值设为text/plain。
try {
String contentType="text/plain";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
//Workbook to byte[]
byte[] barray = bos.toByteArray();
//byte[] to InputStream
InputStream is = new ByteArrayInputStream(barray);
//InputStream to MultipartFile
MultipartFile multipartFile = new MockMultipartFile("测试文件.xlsx", "测试文件.xlsx", contentType, is);
} catch (Exception e) {
e.printStackTrace();
}