How to set content type while uploading in to alfresco?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2013 05:49 AM
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?
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?
Labels:
- Labels:
-
Archive
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2013 08:06 AM
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.
I think you need to use getMimetype() API from MimeTypeservice of Alfresco.
Also, if you post your code it will give more idea.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2013 01:18 AM
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" +
"<cmis
bject>\n"+
"<cmis
roperties>\n" +
"<cmis
ropertyString cmis:name='ObjectTypeId'>\n" +
"<cmis:value>document</cmis:value>\n" +
"</cmis
ropertyString>\n" +
"</cmis
roperties>\n" +
"</cmis
bject>\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");
}
}
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" +
"<cmis

"<cmis

"<cmis

"<cmis:value>document</cmis:value>\n" +
"</cmis

"</cmis

"</cmis

"</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");
}
}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2013 06:22 AM
You can use Apache Tika for doing that
Alfresco also use the Apache Tika for content manipulation
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(); } }}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2013 07:47 AM
Hi mitpatoliya,
It is worked. Thanx for the great help.
Please have a look into the posts if you have time :
http://forums.alfresco.com/forum/end-user-discussions/alfresco-share/create-site-selection-dropdown-...
http://forums.alfresco.com/forum/end-user-discussions/alfresco-share/how-create-freemarker-expressio...
It is worked. Thanx for the great help.
Please have a look into the posts if you have time :
http://forums.alfresco.com/forum/end-user-discussions/alfresco-share/create-site-selection-dropdown-...
http://forums.alfresco.com/forum/end-user-discussions/alfresco-share/how-create-freemarker-expressio...
