我正在使用Apache Commons FileUpload庫來上傳文件。我想將InputStream的內容複製到單個字節數組中。我怎麼能這樣做?如何將輸入流的內容複製或存儲到字節數組中?
try {
List<FileItem> items = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input
// type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
out.println("returned");
} else {
// Process form file field (input type="file").
String fieldname = item.getFieldName();
String filename = FilenameUtils.getName(item.getName());
InputStream input = item.getInputStream();
if (fieldname.equals("file")) {
// please help me here.
byte[] allbyte = ???
}
}
}
}
你試過['InputStream.read'](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#read(byte [],%20int,% 20int))? –