因为java中读取流,他的内部光标不会重置在起始位置,至于光标重置的方法,尝试了几次也不行,只能用流的复制
public void importFile(MultipartFile file){
//复制原始流
InputStream inputStream = file.getInputStream();
ByteArrayOutputStream baos = FileUtil.cloneInputStream(inputStream);
for (int i = 0; i < size; i++) {
//循环多少次,从原始流中就取多少次,
InputStream contentStream = new ByteArrayInputStream(baos.toByteArray());
........相关代码
contentStream.close();//关闭流
}
/**
复制流的方法
*/
public static ByteArrayOutputStream cloneInputStream(InputStream input) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
return baos;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
版权声明:本文为mjxin123456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。