Powered By Blogger

Wednesday, October 5, 2011

How to upload file to Alfresco share using java code

Here the siteid is :testFolders
location to upload is : "/aaa/bbbb

It will upload the file from the local disk to the alfresco site testfolders/documentlibrary/aaa/bbb

Here I have passed the ticket to authenticate with the system



/**
*
*/
package com;

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;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;





/**
* @author basanagowda.patil
*
*/
public class FileUpload {

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", destination)
new StringPart("description", description),
new StringPart("siteid", "testFolders"),
new StringPart("containerid", "documentLibrary"),
new StringPart("uploaddirectory", "/aaa/bbbb")


};
mPost.setRequestEntity(
new MultipartRequestEntity(parts, mPost.getParams())
);
int statusCode1 = client.executeMethod(mPost);
System.out.println("statusLine>>>" +statusCode1+"......"+ mPost.getStatusLine()+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";

String alfrescoTiccketURL = "http://localhost:8080/alfresco"+"/service/api/login?u="+"admin"+"&pw="+"admin";




String ticketURLResponse = invokeWebScriptgetRequest(alfrescoTiccketURL);

//ticketURLResponse =
int startindex= ticketURLResponse.indexOf("")+8;
int endindex = ticketURLResponse.indexOf("
");

ticketURLResponse = ticketURLResponse.substring(startindex, endindex);



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

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

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



public static String invokeWebScriptgetRequest(String url){


// Create an instance of HttpClient.
HttpClient client = new HttpClient();

// Create a method instance.
GetMethod method = new GetMethod(url);
/* // Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));*/
String response = null;
try {
// Execute the method.
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}

// Read the response body.
byte[] responseBody = method.getResponseBody();

// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
System.out.println(response);

} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
return response;

}



}

11 comments:

  1. geez... why not format the code?

    ReplyDelete
  2. Hi am getting bellow status , what can i do
    statusLine>>>401......HTTP/1.1 401 Unauthorized

    ReplyDelete
  3. can give jar files also . I am new to java.

    ReplyDelete
  4. Hi, what do yo mean with "Here the siteid is :testFolders"
    thanks

    ReplyDelete
  5. Hi, Can you let how to upload large file like more than 10 mb file using Alfresco API in java code with status of uploading

    ReplyDelete
  6. need an urgent reply ... hi, Basanagowda the code you have specifed above is giving 401 status "unautorized" ... will plz tell it is ? and how to solv it?

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Hi all,

    you need to change

    int startindex= ticketURLResponse.indexOf("")+8;
    int endindex = ticketURLResponse.indexOf("");

    to
    int startindex= ticketURLResponse.indexOf("<ticket>")+8;
    int endindex = ticketURLResponse.indexOf("</ticket>");

    in order to get the correct ticket id

    and

    new StringPart("siteid", "sito"),

    to

    new StringPart("siteid", "YOURSITEID")

    for me it works fine :
    The upload url:::http://localhost:8080/alfresco/service/api/upload?alf_ticket=TICKET_6cc7ea517d37387cb626ec60c46fe672c034c11d
    statusLine>>>200......HTTP/1.1 200 OK{
    "nodeRef": "workspace://SpacesStore/0954587f-d79d-44d2-8d71-9e288fa7212e",
    "fileName": "Pdf_da_web_service-1.pdf",
    "status":
    {
    "code": 200,
    "name": "OK",
    "description": "File uploaded successfully"
    }
    }

    ReplyDelete
  11. Thank you so much for the program ,it helped me a lot at my work.

    ReplyDelete