Загрузка файла из сторонней программы в домашний каталог пользователя.
Стояла задача загрузки файла в домашний каталог пользователя. Долго мучился и сначала научился грузить файл в како-нибудь сайт. Вот пример:
package com.kristall; import java.io.File; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.auth.AuthScope; 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.Credentials; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.GetMethod; /** * @author aaa * */ public class FileUpload { public static void uploadDocument(String authTicket, File fileobj,String filename, String filetype, String description,String destination) { try { // String urlString = "http://10.10.12.116:8080/alfresco/service/api/upload?alf_ticket="+ authTicket; String urlString = "http://10.10.12.116/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), //modify this according to where you wanna put your content new StringPart("siteid","filesimport"), // new StringPart("siteid", "test"), new StringPart("containerid", "documentLibrary"), //new StringPart("uploaddirectory", "/Company Home") }; 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://192.168.100.152:8080/alfresco"+ "/service/api/login?u=" + "admin" + "&pw=" + "admin"; // String alfrescoTiccketURL = "http://10.10.12.116:8080/alfresco"+ "/service/api/login?u=" + "boltaev" + "&pw=" + "boltaev"; String alfrescoTiccketURL = "http://10.10.12.116/alfresco"+ "/service/api/login?u=" + "boltaev" + "&pw=" + "boltaev"; InteractiveAuthentication ticket = new InteractiveAuthentication(); String ticketURLResponse = ticket.getTicket(alfrescoTiccketURL); File f = new File("/home/oleg/test.pdf"); // FileInputStream is=new FileInputStream(f); uploadDocument(ticketURLResponse, f, "test.pdf","application/pdf", "description", null); // uploadDocument("TICKET_3ef085c4e24f4e2c53a3fa72b3111e55ee6f0543",f,"47.bmp","image file","application/jpg","workspace://SpacesStore/65a06f8c-0b35-4dae-9835-e38414a99bc1"); //workspace://SpacesStore/88662918-1a1a-40cc-a0a1-8907a9d5d6fd } }Но все настойчивее стояла задача грузить именно в домашний каталог пользователя и вот решение:
package com.kristall; import java.io.FileInputStream; import java.io.IOException; import java.rmi.RemoteException; import java.util.Properties; import org.alfresco.webservice.authentication.AuthenticationFault; import org.alfresco.webservice.content.Content; import org.alfresco.webservice.content.ContentServiceSoapBindingStub; import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub; import org.alfresco.webservice.repository.UpdateResult; import org.alfresco.webservice.types.CML; import org.alfresco.webservice.types.CMLCreate; import org.alfresco.webservice.types.ContentFormat; import org.alfresco.webservice.types.NamedValue; import org.alfresco.webservice.types.ParentReference; import org.alfresco.webservice.types.Store; import org.alfresco.webservice.util.AuthenticationUtils; import org.alfresco.webservice.util.Constants; import org.alfresco.webservice.util.Utils; import org.alfresco.webservice.util.WebServiceFactory; import org.alfresco.webservice.types.Reference; import org.alfresco.webservice.util.ISO9075; public class UploadContent { public UploadContent(){ //WebServiceFactory.setEndpointAddress("http://10.10.12.116/alfresco/api"); //AuthenticationUtils.startSession("boltaev", "boltaev"); //AuthenticationUtils.endSession(); String fileName=System.currentTimeMillis()+"_myFile.txt"; Store STORE=new Store(Constants.WORKSPACE_STORE,"SpacesStore"); String ASSOC_CONTAINS="{http://www.alfresco.org/model/content/1.0}contains"; try{ // для версии до 4.2.e // WebServiceFactory.setEndpointAddress("http://10.10.12.116/alfresco/api"); // 4.2.d WebServiceFactory.setEndpointAddress("http://10.10.12.116/alfresco/soapapi"); AuthenticationUtils.startSession("boltaev","boltaev"); //Get the content service ContentServiceSoapBindingStub contService=WebServiceFactory.getContentService(); //Create a parent reference // ParentReference parRef=new ParentReference(STORE,null,"/app:company_home/st:sites"+"/cm:filesimport/cm:documentLibrary",ASSOC_CONTAINS,"{"+Constants.NAMESPACE_CONTENT_MODEL+"}"+fileName); // ParentReference parRef=new ParentReference(STORE,null,"/app:company_home/st:sites"+"/cm:mySite/cm:documentLibrary/"+"cm:Recording/cm:Jan/cm:"+ISO9075.encode("20101130"),ASSOC_CONTAINS,"{"+Constants.NAMESPACE_CONTENT_MODEL+"}"+fileName); // ParentReference parRef=new ParentReference(STORE,null,"/app:company_home/cm:FSetup",ASSOC_CONTAINS,"{"+Constants.NAMESPACE_CONTENT_MODEL+"}"+fileName); ParentReference parRef=new ParentReference(STORE,null,"/app:company_home/app:user_homes/cm:boltaev",ASSOC_CONTAINS,"{"+Constants.NAMESPACE_CONTENT_MODEL+"}"+fileName); if (parRef!= null) System.out.println("Not null!!!"); else System.out.println("Null!!!!"); // ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(); // ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8"); // String docText = "This is a sample document called "; // Content docContentRef = contentService.write(docRef, Constants.PROP_CONTENT, docText.getBytes(), contentFormat); //Define the content format for the content //we are adding ContentFormat contentFormat=new ContentFormat("text/plain","UTF-8"); NamedValue[] properties=new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME,fileName)}; CMLCreate create=new CMLCreate("1",parRef,null,null,null,Constants.TYPE_CONTENT,properties); CML cml=new CML(); cml.setCreate(new CMLCreate[]{create}); System.out.println("1111!!!!!!!!!!!!!!!!!!!!!!!!!!"); ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(); RepositoryServiceSoapBindingStub repService =WebServiceFactory.getRepositoryService(); if (repService!=null) System.out.println("not null"); else System.out.println("null"); UpdateResult[] result=repService.update(cml); //UpdateResult[] result=WebServiceFactory.getRepositoryService().update(cml); System.out.println("2222!!!!!!!!!!!!!!!!!!!!!!!!!!"); Reference newContentNode=result[0].getDestination(); System.out.println("3333!!!!!!!!!!!!!!!!!!!!!!!!!!"); Content content=contService.write(newContentNode,Constants.PROP_CONTENT,"JustSomeText".getBytes(),contentFormat); System.out.println("4444!!!!!!!!!!!!!!!!!!!!!!!!!!"); AuthenticationUtils.endSession(); } catch(AuthenticationFault e) { e.printStackTrace();} catch(RemoteException e){e.printStackTrace();} } public static void main(String...args){ try{ UploadContent upCont=new UploadContent();} catch(Exception ex){ex.printStackTrace();} } }