Java Reference
In-Depth Information
return new
ForwardResolution(COMPOSE);
}
private void
addAttachment(FileBean fileBean)
throws
Exception {
Attachment attachment =
new
Attachment();
attachment.setFileName(fileBean.getFileName());
attachment.setContentType(fileBean.getContentType());
attachment.setSize(fileBean.getSize());
attachmentDao.save(attachment);
Í
fileBean.save(
new
File(attachmentDao.getFilePath(attachment)));
getMessage().addAttachment(attachment);
}
We have to guard against a few
null
s and zeroes. First, we must check
that the
attachments
property is not
null
(
Ê
), because the user may have
clicked the
Upload
button without filling in any of the attachments
fields. Since attachments are not required fields, we just do nothing in
this case.
Next, when looping through the items in the list, we have to make sure
the
FileBean
object is not
null
(
Ë
). The user may have entered a filename
in the second attachment field but left the first one blank. As we saw
earlier regarding the use of indexed properties with lists, the first item
would be
null
in this case.
Finally, if the user enters an invalid file path in the attachment field, a
FileBean
object of size
0
appears in the list. So, we check for that (
Ì
) and
produce a validation error if we encounter a zero-sized
FileBean
.
When all is well and good, the uploaded file is added to the email as an
Attachment
object, which is a simple representation of an attachment:
package
stripesbook.model;
public class
Attachment
extends
ModelBase {
private
String fileName;
private long
size;
private
String contentType;
/
*
getters and setters...
*
/
}
At
Í
, the actual file is saved in a directory on the server. The attachment
DAO takes care of the details of managing the directories and filenames
used for attachments.
Meanwhile back on the Message Compose page, the user has uploaded
attachments. They are shown with an icon, which, when clicked, will
Search WWH ::
Custom Search