cancel
Showing results for 
Search instead for 
Did you mean: 

How to set content type while uploading in to alfresco?

shibu
Champ in-the-making
Champ in-the-making
I am trying to upload files in to alfresco using API. Creating an xml input to the API.
Mime type is setting like,

            Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
       MimeType mimeType = MimeUtil.getMostSpecificMimeType(mimeTypes);
       if(mimeType == null) mimeType = MimeUtil2.UNKNOWN_MIME_TYPE; 

and using "mimeType.toString()" to set the content type.  As it is giving "application/octet-stream" mimetype for most of the files, the mime type is not setting exactly while uploading.

How can I solve it?
4 REPLIES 4

mitpatoliya
Star Collaborator
Star Collaborator
The MimeUtil class which you are using is not a alfresco class right?
I think you need to use getMimetype() API from MimeTypeservice of Alfresco.
Also, if you post your code it will give more idea.

shibu
Champ in-the-making
Champ in-the-making
No, i am not using alfreco class. Its a simple POJO. Images and pdfs are uploading properly. Other file's mime type is reading as "application/octet-stream". So it is not setting properly in alfresco. I will post my code here.

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Collection;

import javax.activation.MimetypesFileTypeMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;


import sun.misc.BASE64Encoder;
import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.MimeUtil2;


public class Upload {

   public static void main(String[] args) throws Exception {
      long start = System.currentTimeMillis();
 
      String host = "127.0.0.1";
      int port = 8080;
      String username = "admin";
      String password = "admin";
      String parentFolder = "app:company_home/sites/my-site/documentLibrary";
      String filePath = "/home/shibu/Desktop/Alfresco Doccument.odt";
      String description = "PDF upload test";
      String author = "admin";
      String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children";
   
      String contentType = "application/atom+xml;type=entry";

      BASE64Encoder encoder = new BASE64Encoder();
      
      File file = new File(filePath);
       FileInputStream fis = new FileInputStream(file);
       DataInputStream dis = new DataInputStream(fis);
       // Create the byte array to hold the data
       byte[] bytes = new byte[(int) file.length()];
       dis.readFully(bytes);

       Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
       MimeType mimeType = MimeUtil.getMostSpecificMimeType(mimeTypes);
       if(mimeType == null) mimeType = MimeUtil2.UNKNOWN_MIME_TYPE;
       
       MimetypesFileTypeMap mfm = new MimetypesFileTypeMap();
      
       String xml =
            "<?xml version='1.0' encoding='utf-8'?>\n" +
            "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" +
            "<title>" + file.getName() + "</title>\n" +
            "<summary>" + description + "</summary>\n" +
            "<author>" + author + "</author>\n" +
            "<content type='" + mimeType.toString()+ "'>" + encoder.encode(bytes) + "</content>\n" +
            "<cmisSmiley Surprisedbject>\n"+
            "<cmisSmiley Tongueroperties>\n" +
            "<cmisSmiley TongueropertyString cmis:name='ObjectTypeId'>\n" +
            "<cmis:value>document</cmis:value>\n" +
            "</cmisSmiley TongueropertyString>\n" +
            "</cmisSmiley Tongueroperties>\n" +
            "</cmisSmiley Surprisedbject>\n" +
            "</entry>\n";

       System.out.println(mimeType.toString());
       
         System.out.println(mfm.getContentType(file));
         FileNameMap fileNameMap = URLConnection.getFileNameMap();
         String type = fileNameMap.getContentTypeFor("file.xls");
         
         
         System.out.println(file.toURL().openConnection().getContentType().toString());
         
         System.out.println("sadas" + type);
         
      DefaultHttpClient httpclient = new DefaultHttpClient();

      httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(host, port),
            new UsernamePasswordCredentials(username, password));

      HttpPost httppost = new HttpPost(url);
      httppost.setHeader("Content-type", contentType);

      StringEntity requestEntity = new StringEntity(xml, "UTF-8");
      
      httppost.setEntity(requestEntity);
      System.out.println("Length : "+requestEntity.getContentLength());
      
      System.out.println("executing request" + httppost.getRequestLine());
      
      HttpResponse response = httpclient.execute(httppost);
      
      HttpEntity entity = requestEntity;

      System.out.println("—————————————-");
      System.out.println(response.getStatusLine());
      
      if (entity != null) {
         System.out.println("Response content type: " + entity.getContentType());
         long contentLength = entity.getContentLength();
         System.out.println("Response content length: "
               + entity.getContentLength());

         
         if (contentLength > 0) {
            
            byte [] b = new byte[(int) contentLength];
            entity.getContent().read(b);
            System.out.println("Response content: " + new String(b));
         }

         entity.writeTo(System.out);
      }

      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      httpclient.getConnectionManager().shutdown();
      long end = System.currentTimeMillis();
      System.out.println("Time spend: " + (end-start) + "ms");
   }
   
   
}

mitpatoliya
Star Collaborator
Star Collaborator
You can use Apache Tika for doing that
Alfresco also use the Apache Tika for content manipulation


import java.io.File;
import java.io.FileInputStream;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

public class Main {

    public static void main(String args[]) throws Exception {

    FileInputStream is = null;
    try {
      File f = new File("C:/Temp/mime/test.docx");
      is = new FileInputStream(f);

      ContentHandler contenthandler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
      Parser parser = new AutoDetectParser();
      // OOXMLParser parser = new OOXMLParser();
      parser.parse(is, contenthandler, metadata);
      System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
      System.out.println("content: " + contenthandler.toString());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
        if (is != null) is.close();
    }
  }
}