I guess you are not using Alfresco SDK for your development. If u are not using SDK, first download SDK then you will get some examples. After that ur development becomes easy.
Any way, I hope the following code will help you.
public static void uploadFile(){
String msg="";
try {
String fileName="";
String title="testfile";
String description="xml file";
//String path = "C:/LL Data/ATAD/Alfresco-Labs-3Stable-OOo-Setup.exe";
String path = "C:/application-context.xml";
path = path.replaceAll("\\\\", "/");
fileName = path.substring(path.lastIndexOf('/')+1,path.length());
String ext= fileName.substring(fileName.lastIndexOf('.')+1,fileName.length());
System.out.println("FileName ==== >> "+fileName);
System.out.println("Path ==== >> "+path);
String contType="";
if(ext.equals("txt"))
contType = "text/plain";
else if (ext.equals("xls"))
contType = "application/vnd.ms-excel";
else if (ext.equals("doc"))
contType = "application/msword";
else if (ext.equals("html") || ext.equals("htm"))
contType = "text/html";
else if (ext.equals("jpg") || ext.equals("jpeg"))
contType = "image/jpeg";
else if (ext.equals("bmp"))
contType = "image/bmp";
else if (ext.equals("pdf"))
contType = "application/pdf";
else if (ext.equals("ppt"))
contType = "application/vnd.ms-powerpoint";
else if(ext.equals("xml"))
contType = "text/xml";
else if (ext.equals("zip"))
contType = "application/vnd.ms-zip";
else if (ext.equalsIgnoreCase("wm"))
contType = "video/x-ms-wm";
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
RepositoryServiceSoapBindingStub repositoryService = null;
Reference content = null;
if(contType.equals(""))
{
contType = getMimeType(fileName);
}
if(contType.equals(""))
{
msg="Unknown file format. File not uploded.";
}
else
{
AuthenticationUtils.startSession("admin", "admin");
// Create a reference to the parent where we want to create content
ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home/app:user_homes", Constants.ASSOC_CONTAINS, null);
repositoryService = WebServiceFactory.getRepositoryService();
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
NamedValue[] contentProps = new NamedValue[1];
NamedValue[] titledProps = new NamedValue[2];
// Assign name
//companyHomeParent.setChildName("cm:" + fileName);
companyHomeParent.setChildName(fileName);
contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, fileName);
// Construct CML statement to add titled aspect
titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, title);
titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, description);
CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_VERSIONABLE, titledProps, null, "2");
// create content
// Construct CML statement to create content node
// Note: Assign "1" as a local id, so we can refer to it in subsequent
// CML statements within the same CML block
CMLCreate create = new CMLCreate("2", companyHomeParent, companyHomeParent.getUuid(), Constants.ASSOC_CONTAINS, null, Constants.PROP_CONTENT, contentProps);
// Construct CML Block
CML cml = new CML();
cml.setCreate(new CMLCreate[] {create});
cml.setAddAspect(new CMLAddAspect[] {addAspect});
// Issue CML statement via Repository Web Service and retrieve result
// Note: Batching of multiple statements into a single web call
try{
UpdateResult[] result = repositoryService.update(cml);
content = result[0].getDestination();
// Write some content
}catch(Exception ex){
System.out.println(" === IN SIDE EXCEPTION === ");
String refstr = "/app:company_home/app:user_homes/"+StringEncription.encode(fileName);
content = new Reference(storeRef, null, refstr);
/*ResultSetRow[] rows = searchFile(fileName,reference,spaces,repositoryService);
String id ="";
if(rows==null){
System.out.println("File was not found in repository!");
}else{
ResultSetRow row = rows[0];
ResultSetRowNode node = row.getNode();
id = node.getId();
System.out.println("id : "+ node.getType() + " , File was found in repository"+rows[0].getNode());
}*/
}
if(content != null){
FileInputStream is = new FileInputStream(path);
byte[] bytes = ContentUtils.convertToByteArray(is);
ContentFormat format = new ContentFormat(contType, "UTF-8");
System.out.println("Reference path =>" + content.getPath());
//Write the content
contentService.write(content, Constants.PROP_CONTENT, bytes, format);
msg=" *** Successfully File Uploaded *** ";
}
}
}
catch(Exception e)
{
msg="Error uploading file";
System.out.println("Error uploading file : "+e);
System.out.println(e.toString());
e.printStackTrace();
}
finally
{
// End the session
AuthenticationUtils.endSession();
}
System.out.println(msg);
}
public static String getMimeType(String filename) {
System.out.println("Mime Type of " + filename + " is " +
new MimetypesFileTypeMap().getContentType(filename));
// expected output :
// "Mime Type of gumby.gif is image/gif"
return new MimetypesFileTypeMap().getContentType(filename);
}
Regards,
Madhu