Java Code Examples for javax.activation.FileDataSource#setFileTypeMap()

The following examples show how to use javax.activation.FileDataSource#setFileTypeMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccessStructure.java    From jplag with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @return A MimeMultipart object containing the zipped result files
 */
public MimeMultipart getResult() {

    File file = new File(JPLAG_RESULTS_DIRECTORY + File.separator
            + submissionID + getUsername() + ".zip");

    MimeMultipart mmp = new MimeMultipart();

    FileDataSource fds1 = new FileDataSource(file);
    MimetypesFileTypeMap mftp = new MimetypesFileTypeMap();
    mftp.addMimeTypes("multipart/zip zip ZIP");
    fds1.setFileTypeMap(mftp);

    MimeBodyPart mbp = new MimeBodyPart();

    try {
        mbp.setDataHandler(new DataHandler(fds1));
        mbp.setFileName(file.getName());

        mmp.addBodyPart(mbp);
    } catch (MessagingException me) {
        me.printStackTrace();
    }
    return mmp;
}
 
Example 2
Source File: cfMAIL.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private void addAttachments( Enumeration<fileAttachment> _attach, Multipart _parent, boolean _isInline ) throws MessagingException{
	while ( _attach.hasMoreElements() ){
		fileAttachment nextFile = _attach.nextElement();
		FileDataSource fds 			= new FileDataSource( nextFile.getFilepath() );
		String mimeType = nextFile.getMimetype();
		if (mimeType == null){
			// if mime type not supplied then auto detect
			mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(nextFile.getFilepath());
     }else{
			// since mime type is not null then it the mime type has been set manually therefore
			// we need to ensure that any call to the underlying FileDataSource.getFileTypeMap()
			// returns a FileTypeMap that will map to this type
			fds.setFileTypeMap(new CustomFileTypeMap(mimeType));
		}
		
		String filename = cleanName(fds.getName());
		try {
			// encode the filename to ensure that it contains US-ASCII characters only
			filename = MimeUtility.encodeText( filename, "utf-8", "b" );
		} catch (UnsupportedEncodingException e5) {
			// shouldn't occur
		}
	  MimeBodyPart mimeAttach	= new MimeBodyPart();
	  mimeAttach.setDataHandler( new DataHandler(fds) );
		mimeAttach.setFileName( filename );

		ContentType ct = new ContentType(mimeType);
		ct.setParameter("name", filename );
		
		mimeAttach.setHeader("Content-Type", ct.toString() );

		if ( _isInline ){
       mimeAttach.setDisposition( "inline" );
       mimeAttach.addHeader( "Content-id", "<" + nextFile.getContentid() + ">" );
		}
     
		_parent.addBodyPart(mimeAttach);
	}
}
 
Example 3
Source File: MimeMessageHelper.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Add an inline element to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param contentId the content ID to use. Will end up as "Content-ID" header
 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #setText
 * @see #addInline(String, org.springframework.core.io.Resource)
 * @see #addInline(String, javax.activation.DataSource)
 */
public void addInline(String contentId, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addInline(contentId, dataSource);
}
 
Example 4
Source File: MimeMessageHelper.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Add an attachment to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * @param attachmentFilename the name of the attachment as it will
 * appear in the mail
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
 * @see #addAttachment(String, javax.activation.DataSource)
 */
public void addAttachment(String attachmentFilename, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addAttachment(attachmentFilename, dataSource);
}
 
Example 5
Source File: MimeMessageHelper.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Add an inline element to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param contentId the content ID to use. Will end up as "Content-ID" header
 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #setText
 * @see #addInline(String, org.springframework.core.io.Resource)
 * @see #addInline(String, javax.activation.DataSource)
 */
public void addInline(String contentId, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addInline(contentId, dataSource);
}
 
Example 6
Source File: MimeMessageHelper.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Add an attachment to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * @param attachmentFilename the name of the attachment as it will
 * appear in the mail
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
 * @see #addAttachment(String, javax.activation.DataSource)
 */
public void addAttachment(String attachmentFilename, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addAttachment(attachmentFilename, dataSource);
}
 
Example 7
Source File: MimeMessageHelper.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Add an inline element to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param contentId the content ID to use. Will end up as "Content-ID" header
 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #setText
 * @see #addInline(String, org.springframework.core.io.Resource)
 * @see #addInline(String, javax.activation.DataSource)
 */
public void addInline(String contentId, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addInline(contentId, dataSource);
}
 
Example 8
Source File: MimeMessageHelper.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Add an attachment to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * @param attachmentFilename the name of the attachment as it will
 * appear in the mail
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
 * @see #addAttachment(String, javax.activation.DataSource)
 */
public void addAttachment(String attachmentFilename, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addAttachment(attachmentFilename, dataSource);
}
 
Example 9
Source File: MimeMessageHelper.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Add an inline element to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param contentId the content ID to use. Will end up as "Content-ID" header
 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #setText
 * @see #addInline(String, org.springframework.core.io.Resource)
 * @see #addInline(String, javax.activation.DataSource)
 */
public void addInline(String contentId, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addInline(contentId, dataSource);
}
 
Example 10
Source File: MimeMessageHelper.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Add an attachment to the MimeMessage, taking the content from a
 * {@code java.io.File}.
 * <p>The content type will be determined by the name of the given
 * content file. Do not use this for temporary files with arbitrary
 * filenames (possibly ending in ".tmp" or the like)!
 * @param attachmentFilename the name of the attachment as it will
 * appear in the mail
 * @param file the File resource to take the content from
 * @throws MessagingException in case of errors
 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
 * @see #addAttachment(String, javax.activation.DataSource)
 */
public void addAttachment(String attachmentFilename, File file) throws MessagingException {
	Assert.notNull(file, "File must not be null");
	FileDataSource dataSource = new FileDataSource(file);
	dataSource.setFileTypeMap(getFileTypeMap());
	addAttachment(attachmentFilename, dataSource);
}