AnsweredAssumed Answered

problem upload file with REST API, StringPart()

Question asked by magarcia_sm on Jun 2, 2015
Latest reply on Aug 12, 2015 by ishaan
Hi there,
I'm trying to upload a file with the REST API, but when uploading not find the file.
I know that the error is in the following lines, I've tried everything but I do not know who put exactly.


new StringPart("siteid", "HULA"),
new StringPart("containerid", "documentLibrary"),
new StringPart("uploaddirectory", "/facultativos")


I saw another post with the same problem but the proposed solutions did not work me.
Any ideas about what could be wrong?
Some indications that put exactly in these fields?

Thanks a lot in advance!!


Code with the message:

response = <?xml version="1.0" encoding="UTF-8"?>
<ticket>TICKET_277b2045c4d75c96e1ced32ecbc15ae307546e91</ticket>

ticket = TICKET_277b2045c4d75c96e1ced32ecbc15ae307546e91
The upload url:::http://localhost:8080/alfresco/service/api/upload?alf_ticket=TICKET_277b2045c4d75c96e1ced32ecbc15ae307546e91
statusLine>>>404……
status line
HTTP/1.1 404 No Encontrado
body
{
    "status" :
  {
    "code" : 404,
    "name" : "Not Found",
    "description" : "Requested resource is not available."
  }, 
 
  "message" : "Site (HULA) not found.", 
  "exception" : "",
 
  "callstack" :
  [
       
  ],
 
  "server" : "Community v5.0.0 (c r91299-b145) schema 8.009",
  "time" : "02-jun-2015 13:40:32"
}



Code Upload.java:


package Conexion_HULA;

import java.io.File;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

public class Upload {

   public static void uploadDocument(String authTicket, File fileobj,
         String filename, String filetype, String description,
         String destination) {
      try {

         String urlString = "http://localhost:8080/alfresco/service/api/upload?alf_ticket='+ authTicket;
         System.out.println("The upload url:::" + urlString);
         HttpClient client = new HttpClient();

         PostMethod mPost = new PostMethod(urlString);
         // File f1 =fileobj;
         Part[] parts = {
               new FilePart("filedata", filename, fileobj, filetype, null),
               new StringPart("filename", filename),
               new StringPart("description", description),
               //new StringPart("destination", "workspace://SpacesStore/50645ce9-6abc-4ea6-93ea-dd7bf65304d6"),
               new StringPart("description", description),

               // modify this according to where you wanna put your content
               new StringPart("siteid", "HULA"),
               new StringPart("containerid", "documentLibrary"),
               new StringPart("uploaddirectory", "/facultativos")
         };
         mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost.getParams()));
         int statusCode1 = client.executeMethod(mPost);
         System.out.println("statusLine>>>" + statusCode1 + "……"
               + "\n status line \n" + mPost.getStatusLine() + "\nbody \n"
               + mPost.getResponseBodyAsString());
         mPost.releaseConnection();

      } catch (Exception e) {
         System.out.println(e);
      }
   }

   public static void main(String args[]) throws IOException {
      // SimpleUpload aw=new SimpleUpload();
      // String Ticket=aw.login();
      // String ticket="TICKET_3e61ccfa8a11690b10e1a2fb0eeee2c5583b0043";

      // aritz : not using predefined method to get credential
      String alfrescoTiccketURL = "http://localhost:8080/alfresco" + "/service/api/login?u=" + "admin" + "&pw=" + "admin";

      Authentication ticket = new Authentication();
      String ticketURLResponse = ticket.getTicket(alfrescoTiccketURL);

      File f = new File("D:/bloqueo1.pdf");

      // FileInputStream is=new FileInputStream(f);
      uploadDocument(ticketURLResponse, f, "bloqueo1.pdf", "application/pdf", "description", null);

      // uploadDocument("TICKET_3ef085c4e24f4e2c53a3fa72b3111e55ee6f0543",
      // f,"47.bmp","image file","application/jpg","workspace://SpacesStore/65a06f8c-0b35-4dae-9835-e38414a99bc1");
   }
}

Outcomes