cancel
Showing results for 
Search instead for 
Did you mean: 

Upload File in User Authenticated Web Script

scoppola
Champ in-the-making
Champ in-the-making
Hi all,
i'm trying to upload a file with http client to my WebScript configured as authenticated by user (so alf_ticket).
My problem is parameters sending through http client, it seem the code is correct, any help?
I user Apache Http Client to do call to alfresco web script

   public static String doUploadCall(String ticket, Map<String, Object>  parameters, UploadFile file, String url)throws HttpException, IOException{
         HttpClient client = new HttpClient();
      
         PostMethod method = new PostMethod(url);     
        
         method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
        
         JSONObject json = JSONObject.fromObject(parameters);
      
         File f = new File("temp");
         OutputStream output = new FileOutputStream(f);
         IOUtils.copy(file.getInpuStream(), output);
      
        
         Part[] parts = {
               new StringPart("alf_ticket", ticket),
               new StringPart("parameters", json.toString()),
               new FilePart(file.getFileName(), f)
         };
        
         method.setRequestEntity(
               new MultipartRequestEntity(parts, method.getParams())
      );
         
         int statusCode = client.executeMethod(method);
         if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
         }
         return method.getResponseBodyAsString();
   }
2 REPLIES 2

scoppola
Champ in-the-making
Champ in-the-making
For who is intersted, with Alfresco Community 3.0.0 the following code work to upload file:


   public static String doUploadCall(String ticket, Map<String, Object>  parameters, UploadFile file, String url)throws HttpException, IOException{
         HttpClient client = new HttpClient();
      
         PostMethod method = new PostMethod(url + "?alf_ticket=" + ticket);     
        
         method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
        
         JSONObject json = JSONObject.fromObject(parameters);
      
         File f = new File(file.getFileName());
         OutputStream output = new FileOutputStream(f);
         IOUtils.copy(file.getInpuStream(), output);
      
        
         Part[] parts = {
               new StringPart("alf_ticket", ticket),
               new StringPart("parameters", json.toString()),
               new FilePart("file", f)
         };
        
         method.setRequestEntity(
               new MultipartRequestEntity(parts, method.getParams())
      );
        
        
         int statusCode = client.executeMethod(method);
         if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
         }
         return method.getResponseBodyAsString();
   }

segzy
Champ in-the-making
Champ in-the-making
In the method doUploadCall
public static String doUploadCall(String ticket, Map<String, Object>  parameters, UploadFile file, String url)throws HttpException, IOException{

Where is the class UploadFile file coming from do you have this class. I am assuming from javazoom. How can I get the api for that, please help.

Cheers