This is the part where I process the message. The same code works fine in version 2.1.4.
Code:
public void receive(MimeMessage message) {
try {
Object content = message.getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart) content);
}
} catch (Exception e) { }
}
private void handleMultipart(Multipart multipart)
throws MessagingException, IOException {
for (int i = 0, n = multipart.getCount(); i < n; i++) {
handlePart(multipart.getBodyPart(i));
}
}
private void handlePart(Part part) throws MessagingException, IOException {
try {
String disposition = part.getDisposition();
if (disposition != null
&& disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
String fileName = part.getFileName();
String[] splitted = fileName.split("\\.");
if (splitted.length > 0 && this.fileEndingFilter
.contains(splitted[splitted.length - 1])) {
InputStream is = part.getInputStream();
File file = saveFile(fileName, is);
List<String> caseIds = this.etl.extractPatientData(file);
notifyChange(caseIds);
}
}
} catch (Exception e) { }
}
private File saveFile(String filename, InputStream input)
throws IOException {
String filePrefix = new Long(System.currentTimeMillis()).toString();
File file = new File(this.archiveDirectory + "/" + filePrefix + "-"
+ filename);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
return file;
}
It always throws an exception when accessing the input stream of the attachment.
InputStream is = part.getInputStream();