@File
2020-04-30T06:44:42.000000Z
字数 1245
阅读 55
java
<form action="http://localhost:8086/lidaye/uploadFile" method="post" enctype="multipart/form-data">
<input type="file" name="imgs">
<input type="file" name="imgs">
<input type="submit">
</form>
MultipartFile 对象
getName()
参数名,对应 input:namegetSize()
文件大小getContentType()
文件类型getOriginalFilename()
文件名包括后缀getResource()
取资源对象 Resource(包含InputStream)getInputStream()
取输入流对象 InputStream(Bytes)getBytes()
取字节数组isEmpty()
是否空文件
@RestController
@RequestMapping("/lidaye")
public class TestComtroller {
@PostMapping("/uploadFile")
public Object uploadFile(@RequestParam List<MultipartFile> imgs) {
imgs.forEach(m -> {
// 拿到流
try(InputStream inputStream = m.getInputStream()) {
// 装文件内容
StringBuilder sb = new StringBuilder();
// 一次读取1024B
byte[] b = new byte[1024];
// 读
while (inputStream.read(b) > 0) {
sb.append(new String(b, Charset.defaultCharset()));
}
// 当前文件的内容
System.out.println(sb);
} catch (IOException e) {
e.printStackTrace();
}
});
return true;
}
}
@RestController
@RequestMapping("/lidaye")
public class TestComtroller {
@PostMapping("/uploadFile")
public Object uploadFile(@RequestParam List<MultipartFile> imgs) {
imgs.forEach(m -> {
try {
// 直接获取到完整的内容
System.out.println(new String(m.getBytes(),Charset.defaultCharset()));
} catch (IOException e) {
e.printStackTrace();
}
});
return true;
}
}