Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.beans; import java.io.InputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.util.Random; import org.apache.commons.lang.StringUtils; /** * The Class EmailAttachmentBean. */ public class EmailAttachmentBean implements java.io.Serializable { /** The email id. */ private int emailId; /** The file name. */ private String fileName; /** The file size. */ private int fileSize; /** The mime type. */ private String mimeType; /** The content. */ private InputStream content; /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** * Sets the email id. * * @param emailIdVal the new email id */ public final void setEmailId(final int emailIdVal) { this.emailId = emailIdVal; } /** * Gets the email id. * * @return the email id */ public final int getEmailId() { return this.emailId; } /** * Sets the file name. * * @param fileNameVal the new file name */ public final void setFileName(final String fileNameVal) { this.fileName = fileNameVal; } /** * Gets the file name. * * @return the file name */ public final String getFileName() { if (this.fileName == null) { this.fileName = ""; } if (StringUtils.isBlank(this.fileName)) { // Generate new filename based on an 13 letter random string Random r = new Random(); final int maxLength = 36; this.fileName = Long.toString(Math.abs(r.nextLong()), maxLength); } return this.fileName; } /** * Sets the file size. * * @param fileSizeVal the new file size */ public final void setFileSize(final int fileSizeVal) { this.fileSize = fileSizeVal; } /** * Gets the file size. * * @return the file size */ public final int getFileSize() { return this.fileSize; } /** * Sets the mime type. * * @param mimeTypeVal the new mime type */ public final void setMimeType(final String mimeTypeVal) { // The mimeType coming in may not be pure and may need some filtering String mime = ""; if (mimeTypeVal != null) { if (mimeTypeVal.indexOf(";") > 0) { mime = mimeTypeVal.substring(0, mimeTypeVal.indexOf(";")); } } this.mimeType = mime; } /** * Gets the mime type. * * @return the mime type */ public final String getMimeType() { if (this.mimeType == null) { return "unknown"; } return this.mimeType; } /** * Sets the content. * * @param contentStream the new content */ public final void setContent(final InputStream contentStream) { this.content = contentStream; } /** * Save content. * * @param directory the directory * * @throws IOException Signals that an I/O exception has occurred. */ public final void saveContent(final String directory) throws IOException { if (this.content != null) { String filename = directory + "/" + getFileName(); OutputStream output = new BufferedOutputStream(new FileOutputStream(filename)); final int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int numRead; while ((numRead = this.content.read(buffer)) != -1) { output.write(buffer, 0, numRead); } } else { throw new IOException("No content to save"); } } }