一. File转byte[ ]
File file = new File("xx");
byte[] buf = null;
try
{
FileInputStream fis = new FileInputStream(file );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] byte = new byte[1024];
int n;
while ((n = fis.read(byte)) != -1)
{
bos.write(byte, 0, n);
}
fis.close();
bos.close();
buf = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buf ;
一. File转byte[ ] 工具类
import java.nio.file.Files;//Files引用
File file = new File("xx");
Files.readAllBytes(file.toPath());
二. byte[ ]转MultipartFile
public void wxUpload(@RequestBody byte[] bytes){
InputStream inputStream = new ByteArrayInputStream(bytes);
MultipartFile file = null;
try {
file = new MockMultipartFile("tmp.png" , "tmp.png" , ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
//MockMultipartFile方法介绍
/**
* @param name 新文件名称
* @param originalFilename 源文件名称
* @param contentType 内容类型
* @param contentStream InputStream流
*/
// MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream);
} catch (Exception e) {
e.printStackTrace();
}
}
版权声明:本文为jlshachq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。