Example usage for javax.activation DataHandler DataHandler

List of usage examples for javax.activation DataHandler DataHandler

Introduction

In this page you can find the example usage for javax.activation DataHandler DataHandler.

Prototype

public DataHandler(URL url) 

Source Link

Document

Create a DataHandler instance referencing a URL.

Usage

From source file:sendfile.java

public static void main(String[] args) {
    if (args.length != 5) {
        System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
        System.exit(1);/*  w w  w.j a v  a 2  s . com*/
    }

    String to = args[0];
    String from = args[1];
    String host = args[2];
    String filename = args[3];
    boolean debug = Boolean.valueOf(args[4]).booleanValue();
    String msgText1 = "Sending a file.\n";
    String subject = "Sending a file";

    // create some properties and get the default Session
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);

    Session session = Session.getInstance(props, null);
    session.setDebug(debug);

    try {
        // create a message
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = { new InternetAddress(to) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(msgText1);

        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource(filename);
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);

        // add the Multipart to the message
        msg.setContent(mp);

        // set the Date: header
        msg.setSentDate(new Date());

        // send the message
        Transport.send(msg);

    } catch (MessagingException mex) {
        mex.printStackTrace();
        Exception ex = null;
        if ((ex = mex.getNextException()) != null) {
            ex.printStackTrace();
        }
    }
}

From source file:com.jt.https.test.send.java

public static void main(String[] args) throws Exception {
    //final String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ChinaTourinsApply xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Head><UserId>0000000000</UserId><CommandId>CTAA0001</CommandId><SeqNo>JT2014_0000000263863</SeqNo></Head><Body><Apply><ApplyId>1201201407256948</ApplyId><TravelAgencyInfo><Id>1129035</Id><Name>???</Name><Address>?45?</Address><Zipcode>310000</Zipcode><Telphone>0571-63700288</Telphone><License>L-ZJ00295</License><OrgCode>79093228-2</OrgCode><OutboundTourismCredentials>N</OutboundTourismCredentials><LastYearTouristAmount>0.0</LastYearTouristAmount><LastYearRevenue>0-600</LastYearRevenue><last3YearAccidents>N</last3YearAccidents><Last1YearAccidentsFee>0.0</Last1YearAccidentsFee><Last2YearAccidentsFee>0.0</Last2YearAccidentsFee><Last3YearAccidentsFee>0.0</Last3YearAccidentsFee><Contact><Name></Name><Telphone>0571-63700288</Telphone><Mail>385816647@qq.ocm</Mail><Fax>0571-63700288</Fax></Contact><Branches /><Location><ProvinceId>12</ProvinceId><ProvinceName>?</ProvinceName><CityId>1201</CityId><CityName>?</CityName></Location><Properties><Key Name=\"LastApplyNo\" /><Key Name=\"LastPolicyNo\" /><Key Name=\"NewNum\" /><Key Name=\"NewLocation\" /></Properties><Last1YearPolicyNoList><Last1YearPolicyNoInfo><UserId /><PolicyNo /></Last1YearPolicyNoInfo></Last1YearPolicyNoList></TravelAgencyInfo><InsurancePolicyInfo><BeginDate>20140801</BeginDate><EndDate>20141231</EndDate><InsuranceDays>153</InsuranceDays><Renewal>N</Renewal><IssuingCompanyId>2014004</IssuingCompanyId><IssuingCompanyName>?????</IssuingCompanyName><IssuingGeneralCompanyName>???</IssuingGeneralCompanyName><RetroactiveDate>20140801</RetroactiveDate><MainInsurance><EachAccidentLiabilityLimit>20141000</EachAccidentLiabilityLimit><EachAccidentEveryLiabilityLimit>20142004</EachAccidentEveryLiabilityLimit><Properties><Key Name=\"MainPremium\">3135.4509</Key></Properties></MainInsurance><AdditionalInfo><Properties /></AdditionalInfo><PayDate>20140730</PayDate><PremiumInfo><PremiumAdjustmentFactor><YearTouristAmountRatio>-0.15</YearTouristAmountRatio><DistrictRatio>0</DistrictRatio><EachAccidentEveryLiabilityLimitRatio>0.1</EachAccidentEveryLiabilityLimitRatio><LossRatio>0</LossRatio><PreviousLossRatio>0</PreviousLossRatio><AdditionalRatio>0</AdditionalRatio><RiskControlRatio>0</RiskControlRatio><LoyaltyRatio>0</LoyaltyRatio><CoverageRateSystem>0</CoverageRateSystem><DatePreferentialRatio /></PremiumAdjustmentFactor><PremiumDistribution><TotalPremium>3135.45</TotalPremium><Distribution><RenBao CompanyId=\"2014004\">1442.30</RenBao><TaiBao CompanyId=\"2014058\">501.67</TaiBao><RenShou CompanyId=\"2014049\">250.84</RenShou><PingAn CompanyId=\"2014060\">344.90</PingAn><DaDi CompanyId=\"2014045\">282.19</DaDi><TaiPing CompanyId=\"2014039\">313.55</TaiPing></Distribution></PremiumDistribution><PremiumAdjust><RiskControl>0</RiskControl><Prepaid /></PremiumAdjust></PremiumInfo></InsurancePolicyInfo></Apply></Body></ChinaTourinsApply>";

    final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ChinaTourinsResponse><Head><UserId>String</UserId><CommandId>String</CommandId><SeqNo>String</SeqNo><Ref-SeqNo>String</Ref-SeqNo></Head><Body><Apply><ApplyId>String</ApplyId><TravelAgencyId>String</TravelAgencyId><ResponseCode>String</ResponseCode><ResponseMessage>String</ResponseMessage><PolicyNo>String</PolicyNo><UpdateNo>String</UpdateNo><Fee>String</Fee><IssuingDate>String</IssuingDate><ElectricalDocument>String</ElectricalDocument></Apply></Body><IssuingCompany><Name>String</Name><Address>String</Address><Zipcode>String</Zipcode><Telphone>String</Telphone><Contact><Name>String</Name><Telphone>String</Telphone><Mail>String</Mail><Fax>String</Fax></Contact><Ccheck>String</Ccheck><Cbill>String</Cbill><Agent>String</Agent></IssuingCompany></ChinaTourinsResponse>";

    WebInsuranceExchangeStub client = new WebInsuranceExchangeStub(
            "http://localhost/exchange/webservices/webInsuranceExchange");
    com.jt.https.test.WebInsuranceExchangeStub.PolicyApplyConfirm post = new com.jt.https.test.WebInsuranceExchangeStub.PolicyApplyConfirm();
    post.setUserId("");
    DataHandler param = new DataHandler(new DataSource() {
        public InputStream getInputStream() {
            return new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8")));
        }/*  www.  j av  a 2  s  . co m*/

        public OutputStream getOutputStream() {
            return null;
        }

        public String getContentType() {
            return "";
        }

        public String getName() {
            return "";
        }
    });
    post.setConfirmMessage(param);
    com.jt.https.test.WebInsuranceExchangeStub.PolicyApplyConfirmResponse response = client
            .policyApplyConfirm(post);
    InputStream is = response.getResponseMessage().getInputStream();
    String xml_result = ToolsUtils.ConvertStreamToString(is);
    System.out.println(xml_result);
    //PostTo(xml);
}

From source file:es.caib.sgtsic.util.DataHandlers.java

public static DataHandler byteArrayToDataHandler(byte[] arrayByte, String mimetype) {

    ByteArrayDataSource dataSource = new ByteArrayDataSource(arrayByte, mimetype);
    return new DataHandler(dataSource);

}

From source file:es.caib.sgtsic.util.DataHandlers.java

public static DataHandler byteArrayToDataHandler(byte[] arrayByte) {

    InputStream is = new BufferedInputStream(new ByteArrayInputStream(arrayByte));

    String mimetype = "";

    try {/*ww w  .  ja  va  2s  .c  o m*/
        mimetype = URLConnection.guessContentTypeFromStream(is);
    } catch (IOException ex) {
        Logger.getLogger(DataHandlers.class.getName()).log(Level.SEVERE, null, ex);
    }

    DataSource dataSource = new ByteArrayDataSource(arrayByte, mimetype);

    return new DataHandler(dataSource);

}

From source file:org.apache.axis.encoding.ser.PlainTextDataHandlerSerializer.java

/**
 * Serialize a Source DataHandler quantity.
 *//*from   ww  w  .  ja  v a2 s.  c  om*/
public void serialize(QName name, Attributes attributes, Object value, SerializationContext context)
        throws IOException {
    DataHandler dh = new DataHandler(new PlainTextDataSource("source", (String) value));
    super.serialize(name, attributes, dh, context);
}

From source file:org.apache.axis.encoding.ser.ImageDataHandlerSerializer.java

/**
 * Serialize a Source DataHandler quantity.
 *///  ww  w. j av a  2s .com
public void serialize(QName name, Attributes attributes, Object value, SerializationContext context)
        throws IOException {
    DataHandler dh = new DataHandler(new ImageDataSource("source", (Image) value));
    super.serialize(name, attributes, dh, context);
}

From source file:org.apache.axis.encoding.ser.OctetStreamDataHandlerSerializer.java

/**
 * Serialize a Source DataHandler quantity.
 *//*from w  w  w  .j av  a 2 s  .c o m*/
public void serialize(QName name, Attributes attributes, Object value, SerializationContext context)
        throws IOException {
    DataHandler dh = new DataHandler(new OctetStreamDataSource("source", (OctetStream) value));
    super.serialize(name, attributes, dh, context);
}

From source file:org.apache.axis.encoding.ser.MimeMultipartDataHandlerSerializer.java

/**
 * Serialize a Source DataHandler quantity.
 *///from   www .  j ava  2 s . c om
public void serialize(QName name, Attributes attributes, Object value, SerializationContext context)
        throws IOException {
    if (value != null) {
        DataHandler dh = new DataHandler(new MimeMultipartDataSource("Multipart", (MimeMultipart) value));
        super.serialize(name, attributes, dh, context);
    }
}

From source file:com.nortal.jroad.model.XRoadAttachment.java

public XRoadAttachment(String cid, String contentType, byte[] data) {
    this.cid = cid;
    this.dataHandler = new DataHandler(new ByteArrayDataSource(contentType, data));
}

From source file:org.wso2.carbon.esb.security.policy.ESBJAVA3899_PolicyReferenceInWSDLBindingsTestCase.java

@BeforeClass
protected void init() throws Exception {
    super.init();
    uploadResourcesToConfigRegistry();//  ww w  .  j a  v a  2s .co m
    uploadCapp(carFileName, new DataHandler(new URL("file:" + File.separator + File.separator
            + getESBResourceLocation() + File.separator + "car" + File.separator + carFileName)));
    TimeUnit.SECONDS.sleep(5);
    log.info(carFileName + " uploaded successfully");

}