Skip to main content
Published: June 09 2020, 12:24:00 PMUpdated: November 28 2020, 11:47:26 PM

UploadSiteHostedPictures sample in JAVA

Summary

 UploadSiteHostedPicture sample implemented in Java using Apache HttpClient library.


Detailed Description

  The attached standalone sample project contains an implementation of the UploadSiteHostedPictures call. The project illusturates how to use Apache HttpClient to send  a binary attachment, an image file to the target eBay EPS server:

   Sandbox :       https://api.sandbox.ebay.com/ws/api.dll
   Production:     https://api.ebay.com/ws/api.dll

 Before run the sample project,  you will need to:

1. download the Apache Commons HttpClient, Logging Codec and JDOM libraries :

     commons-codec-1.3.jar
     commons-httpclient-version.jar
     commons-logging.jar
     commons-logging-api.jar 
     jdom.jar

These java jar files can be obtained from

Apache Jakarta Commons HttpClient Apache Jakarta Commons Logging Comm Apache Codec and JDOM  sites

2. open ./config.properties and fill out the following properties

# Your Application ID
AppID=
# Your Developer ID
DevID=
# Your Developer Cert string
Cert=
# eBay EPS server url
EPS_ServerURL=
# the image file that to be uploaded
ImageFile=
# the file that saves UploadSiteHostedPictureResponse document
OutputFile=
# an valid token for firing the UploadSiteHostedPicture request

token=

Note.   A README.txt included in the sample project documents the usage of the project


 Here are the main functions that showing the actual code of uploading picture via UploadSiteHostedPicutes API with  HTTP post operation in the attached sample project:

/*
* © 2007-2013 eBay Inc., All Rights Reserved
* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php
*/

package cpm.ebay.api.attachment.client;
/*
* CallerClient.java
*
*/


import java.io.*;
import java.util.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;

public class CallerClient {
    public static String PICTURE_UPLOAD = "PicUpload";
    public static long totalTime;
    private String m_imageFileName;
    private String m_serverURL;
    private String m_request;
    private String m_type;
    private String m_devId;
    private String m_appId;
    private String m_cert;
    private String m_outputFile;


    public CallerClient(String request, Properties props, String type) {

        m_imageFileName = props.getProperty("ImageFile");
        m_serverURL = props.getProperty("EPS_ServerURL");
        m_request = request;
        m_type = type;
        m_devId = props.getProperty("DevID");
        m_appId =props.getProperty("AppID");
        m_cert=props.getProperty("Cert");
        m_outputFile = props.getProperty("OutputFile");
    } 

/*
*  CallerClient.execute() method
1. creates HttpClient and PostMethod   objects 
*  2. specifies HTTP header with the parameters that required for sending the UploadSiteHostedPictures API 
*  request
*  3. makes Part array object 
*  4. executes the Http Post operation 
*  5. saves returned UploadSiteHostedPictures response XML document to a local file

*/

    public void execute() {
        HttpClient client = new HttpClient();
        PostMethod filePost = new PostMethod(m_serverURL);
        try {

            if(PICTURE_UPLOAD.equals(m_type)) {
                filePost.addRequestHeader("X-EBAY-API-CALL-NAME","UploadSiteHostedPictures");
                // include both xml message and an attachment
                Part[] parts = UploadAttachment.uploadFileAsAttachment(m_imageFileName,m_request);
                if(parts != null){
                    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
                }else{
                // set UTF-8 encoding
                    StringRequestEntity req = new StringRequestEntity(m_request,"text/plain","UTF-8");
                    filePost.setRequestEntity(req);
                }
            }
            filePost.addRequestHeader("SOAPAction","");
            filePost.addRequestHeader("X-EBAY-API-SESSION-CERTIFICATE",m_appId+";"+m_devId+";"+m_cert);
            filePost.addRequestHeader("X-EBAY-API-COMPATIBILITY-LEVEL","685");
            filePost.addRequestHeader("X-EBAY-API-DEV-NAME",m_devId);
            filePost.addRequestHeader("X-EBAY-API-APP-NAME",m_appId);
            filePost.addRequestHeader("X-EBAY-API-CERT-NAME",m_cert);
            filePost.addRequestHeader("X-EBAY-API-SITEID","0");
            filePost.addRequestHeader("X-EBAY-API-DETAIL-LEVEL","0");
            long startTime = System.currentTimeMillis();
            // send messages
            int status = client.executeMethod(filePost);

            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;
            totalTime += elapsedTime;
            System.out.println("It took " + elapsedTime + "ms to execute this API\n");
             // redirect the XML response to a local file
            saveResponseToFile(filePost,m_outputFile);

        } catch(Exception ex) {
            ex.toString();

        }
    }


   public static void main(String[] args) {
        Properties config = new Properties();
        String propFile = "config.properties";
        try {
            config.load(new FileInputStream(propFile));
        }catch(IOException ie) {
            ie.toString();
        }
        try{
            String token = config.getProperty("token");  String requestStr =readXmlRequest(token);
            System.out.println("request" + requestStr);
            CallerClient client = new CallerClient(requestStr,config,CallerClient.PICTURE_UPLOAD);
            client.execute();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveResponseToFile(PostMethod filePost, String outputFile){
        //Get data as a String
        try {
            System.out.println( filePost.getRequestHeaders());
            System.out.println(filePost.getResponseBodyAsString());
            //OR as a byte array
            byte [] res  = filePost.getResponseBody();
            //write to file
            FileOutputStream fos= new FileOutputStream(outputFile);
            fos.write(res);

            //release connection
            filePost.releaseConnection();
        }catch (Exception es){ // handle exception here
        }
  }
    private static String readXmlRequest(String token){
        String  uploadPicturesXML= " <?xml version=\"1.0\" encoding=\"utf-8\"?> \n"+
                "<UploadSiteHostedPicturesRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\"> \n\t"+
                "<RequesterCredentials> \n\t\t" +
                " <ebl:eBayAuthToken xmlns:ebl=\"urn:ebay:apis:eBLBaseComponents\">"+token+"</ebl:eBayAuthToken>\n\t\t"+
                "</RequesterCredentials>\n\t" +
                "<PictureName>testPic</PictureName>\n<PictureSet>Supersize</PictureSet>\n"+ 
                "</UploadSiteHostedPicturesRequest>\n";
        return uploadPicturesXML;
    }

Version Info

The attached project is based on the following version:

XML API version 513

How well did this answer your question?
Answers others found helpful