将以下C#代码翻译为Java的 private static List<byte[]> ReadFiles(IEnumerable<HttpPostedFileBase> files) { var streams = new List<byte[]>(); var buffer = new byte[128 * 1024]; foreach (var file in files) { if (file.ContentLength == 0) continue; var ms = new MemoryStream(); int read; while ((read = file.InputStream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } streams.Add(ms.ToArray()); } return streams; }
时间: 2024-02-10 15:12:07 浏览: 117
下面是Java代码:
```
private static List<byte[]> readFiles(List<Part> files) throws IOException {
List<byte[]> streams = new ArrayList<>();
byte[] buffer = new byte[128 * 1024];
for (Part file : files) {
if (file.getSize() == 0) continue;
ByteArrayOutputStream ms = new ByteArrayOutputStream();
int read;
try (InputStream inputStream = file.getInputStream()) {
while ((read = inputStream.read(buffer, 0, buffer.length)) > 0) {
ms.write(buffer, 0, read);
}
}
streams.add(ms.toByteArray());
}
return streams;
}
```
注意:Java中的`Part`代表了上传的文件,需要在方法参数中声明。此外,在使用`ByteArrayOutputStream`时需要在文件读取完成后手动关闭输入流。
阅读全文