I am using the Apache Commons Uploader package, and I am trying to read a file that 开发者_如何学Cis being uploaded. I have a FileItem object.
The first few lines of the file are ASCII and I want to be able to read them and extract the metadata that is contained in them. What is the best method/pattern to go about doing this.
Call getInputStream()
on the FileItem
, wrap that in a BufferedReader
, then read your file line by line using BufferedReader.readLine()
until you've read the meta data.
final List<FileItem> files = new ServletFileUpload(new DiskFileItemFactory())
.parseRequest(httpRequest.getWrappedRequest());
final List<Long> images = new ArrayList<>();
for (final FileItem file : files) {
if (file.getFieldName().equals("file")) {
final InputStream stream = file.getInputStream();
final byte[] bytes = IOUtils.toByteArray(stream);
...
file.delete();
}
}
files.clear();
精彩评论