List of usage examples for javax.activation FileDataSource getFile
public File getFile()
From source file:immf.MyHtmlEmail.java
/** * Embeds a file in the HTML.// w w w . ja v a 2s .c om * * <p>This method embeds a file located by an URL into * the mail body. It allows, for instance, to add inline images * to the email. Inline files may be referenced with a * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID * returned by the embed function. Files are bound to their names, which is * the value returned by {@link java.io.File#getName()}. If the same file * is embedded multiple times, the same CID is guaranteed to be returned. * * <p>While functionally the same as passing <code>FileDataSource</code> to * {@link #embed(DataSource, String, String)}, this method attempts * to validate the file before embedding it in the message and will throw * <code>EmailException</code> if the validation fails. In this case, the * <code>HtmlEmail</code> object will not be changed. * * @param file The <code>File</code> to embed * @param cid the Content-ID to use for the embedded <code>File</code> * @return A String with the Content-ID of the file. * @throws EmailException when the supplied <code>File</code> cannot be used * or if the file has already been embedded; * also see {@link javax.mail.internet.MimeBodyPart} for definitions * @since 1.1 */ public String embed(File file, String cid) throws EmailException { if (StringUtils.isEmpty(file.getName())) { throw new EmailException("file name cannot be null or empty"); } // verify that the File can provide a canonical path String filePath = null; try { filePath = file.getCanonicalPath(); } catch (IOException ioe) { throw new EmailException("couldn't get canonical path for " + file.getName(), ioe); } // check if a FileDataSource for this name has already been attached; // if so, return the cached CID value. if (inlineEmbeds.containsKey(file.getName())) { InlineImage ii = (InlineImage) inlineEmbeds.get(file.getName()); FileDataSource fileDataSource = (FileDataSource) ii.getDataSource(); // make sure the supplied file has the same canonical path // as the one already associated with this name. String existingFilePath = null; try { existingFilePath = fileDataSource.getFile().getCanonicalPath(); } catch (IOException ioe) { throw new EmailException("couldn't get canonical path for file " + fileDataSource.getFile().getName() + "which has already been embedded", ioe); } if (filePath.equals(existingFilePath)) { return ii.getCid(); } else { throw new EmailException("embedded name '" + file.getName() + "' is already bound to file " + existingFilePath + "; existing names cannot be rebound"); } } // verify that the file is valid if (!file.exists()) { throw new EmailException("file " + filePath + " doesn't exist"); } if (!file.isFile()) { throw new EmailException("file " + filePath + " isn't a normal file"); } if (!file.canRead()) { throw new EmailException("file " + filePath + " isn't readable"); } return embed(new FileDataSource(file), file.getName()); }
From source file:org.apache.axis.attachments.DimeBodyPart.java
protected long getDataSize(DataHandler dh) { long dataSize = -1L; try {/* www. ja v a 2 s .c om*/ DataSource ds = dh.getDataSource(); //Do files our selfs since this is costly to read in. Ask the file system. // This is 90% of the use of attachments. if (ds instanceof javax.activation.FileDataSource) { javax.activation.FileDataSource fdh = (javax.activation.FileDataSource) ds; java.io.File df = fdh.getFile(); if (!df.exists()) { throw new RuntimeException(Messages.getMessage("noFile", df.getAbsolutePath())); } dataSize = df.length(); } else { dataSize = 0; java.io.InputStream in = ds.getInputStream(); byte[] readbuf = new byte[64 * 1024]; int bytesread; do { bytesread = in.read(readbuf); if (bytesread > 0) dataSize += bytesread; } while (bytesread > -1); if (in.markSupported()) { //Leave the stream open for future reading // and reset the stream pointer to the first byte in.reset(); } else { //FIXME: bug http://nagoya.apache.org/jira/secure/ViewIssue.jspa?key=AXIS-1126 //if we close this then how can we read the file? eh? in.close(); } } } catch (Exception e) { //TODO: why are exceptions swallowed here? log.error(Messages.getMessage("exception00"), e); } return dataSize; }
From source file:org.apache.axis.attachments.MimeUtils.java
/** * Determine the length for the individual part. * @param bp is the part to be searched. * @return the length in bytes.// w w w.j a v a 2 s .c o m */ protected static long getContentLength(javax.mail.internet.MimeBodyPart bp) { long headerLength = -1L; long dataSize = -1L; try { headerLength = getHeaderLength(bp); javax.activation.DataHandler dh = bp.getDataHandler(); javax.activation.DataSource ds = dh.getDataSource(); // Do files our selfs since this is costly to read in. Ask the file system. // This is 90% of the use of attachments. if (ds instanceof javax.activation.FileDataSource) { javax.activation.FileDataSource fdh = (javax.activation.FileDataSource) ds; java.io.File df = fdh.getFile(); if (!df.exists()) { throw new RuntimeException(Messages.getMessage("noFile", df.getAbsolutePath())); } dataSize = df.length(); } else { dataSize = bp.getSize(); if (-1 == dataSize) { // Data size is not known so read it the hard way... dataSize = 0; java.io.InputStream in = ds.getInputStream(); byte[] readbuf = new byte[64 * 1024]; int bytesread; do { bytesread = in.read(readbuf); if (bytesread > 0) { dataSize += bytesread; } } while (bytesread > -1); in.close(); } } } catch (Exception e) { log.error(Messages.getMessage("exception00"), e); } return dataSize + headerLength; }