0 Replies Latest reply on Oct 24, 2013 5:23 AM by arvikumar

    Java code to  create a document with an .xlsx file as attachment.

    arvikumar

      I wanted to share a java code which will create a document with an xlsx file as attachment. The code is below. I hope it will be helpful for others.

       

      import java.io.BufferedReader;

      import java.io.FileInputStream;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.io.OutputStream;

      import java.io.PrintWriter;

      import java.net.HttpURLConnection;

      import java.net.URL;

      import java.net.Proxy;

      import java.net.InetSocketAddress;

      import java.util.ArrayList;

      import java.util.List;

      import java.io.OutputStreamWriter;

       

      public class CurlExample {

       

        public static void main(String[] args) {

          try {

         

          String url = "http://localhost:8080/api/core/v3/contents";

          URL obj = new URL(url);

          HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

          conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=SNIP");   

          conn.setDoOutput(true);

          conn.setRequestMethod("POST");

          String userpass = "username" + ":" + "password";

          String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));

          conn.setRequestProperty ("Authorization", basicAuth);

       

          String dataA =  "--SNIP\r\nContent-Disposition:  form-data; filename=\"Machine.xlsx\";name=\"foo\"\r\nContent-Type:application/json\r\n\r\n{\"type\":\"document\"," +

                  "\"subject\":\"Machine\",\"content\":{\"type\":\"text/html\",\"text\":\"<body><p>Machine data</p></body>\"}}\r\n--SNIP\r\nContent-Disposition: form-data;" +

                  "filename=\"Machine.xlsx\";name=\"foo\"\r\nContent-Type: application/vnd.ms-excel\r\n\r\n";      

         

          String dataC = "\r\n--SNIP--\r\n";   

          PrintWriter writer2;   

           OutputStream outputStream2;

           outputStream2 = conn.getOutputStream();   

           writer2 = new PrintWriter(new OutputStreamWriter(outputStream2, "UTF-8"),true);    

           writer2.append(dataA);

           writer2.flush();

           FileInputStream inputStream = new FileInputStream("e:/Test/Machine.xlsx");

           byte[] buffer = new byte[4096];

           int bytesRead = -1;

           while ((bytesRead = inputStream.read(buffer)) != -1) {

               outputStream2.write(buffer, 0, bytesRead);

           }

           outputStream2.flush();

           inputStream.close();    

           writer2.append(dataC).flush();

           writer2.close();

           List<String> response = new ArrayList<String>();

       

               BufferedReader reader = new BufferedReader(new InputStreamReader(

                       conn.getInputStream()));

               String line = null;

               while ((line = reader.readLine()) != null) {

                   response.add(line);

               }

               reader.close();

               conn.disconnect();

        

       

          } catch (Exception e) {

          e.printStackTrace();

          }

       

        }

      }