How to upload image in Dam using standalone application which will generate metadata? | Community
Skip to main content
New Participant
February 15, 2017
Solved

How to upload image in Dam using standalone application which will generate metadata?

  • February 15, 2017
  • 5 replies
  • 4430 views

Hi,

I have a image in local drive. I want to upload that image to in my AEM dam. For that I have written following code.

public class ThroughHttpPost { public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("D:\\images\\test.jpg"); byte[] bytesArray = new byte[(int) file.length()]; FileInputStream fis; try { fis = new FileInputStream(file); fis.read(bytesArray); //read file into bytes[] fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } DefaultHttpClient httpClient = new DefaultHttpClient();//Client HttpPost postRequest = new HttpPost("http://localhost:4502/content/dam/test");//Post Request to specified URL httpClient.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("admin", "admin")); ByteArrayBody bab = new ByteArrayBody(bytesArray, "test.jpg"); MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("test.jpg", bab); postRequest.setEntity(reqEntity.build()); try { HttpResponse response = httpClient.execute(postRequest); response.getStatusLine(); System.out.println("done"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

I am able to push that image to dam. But the problem is that it is taking the image as nt:file. It doesn't have any metadata as shown in attachment. 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

The outside Java app should only post to an AEM servlet.

So you need 2 parts: 

1 - outside client app (which you have) that posts files to AEM. 

2 - an AEM Sling servlet that uses the AssetManager API

See this article that does this exact use case from a outside Java Swing client:

  • handles multiple files by reading an XML file (defines where the assets are located, etc)
  • posts each file to a Sling servlet that handles the file and uses AssetManager

https://helpx.adobe.com/experience-manager/using/multiple-digital-assets.html

Use of the AssetManager API is the best way when you want to upload files from an outside client. 

5 replies

New Participant
February 16, 2017

Thanks smacdonald. It is working as expected.

smacdonald2008
New Participant
February 15, 2017

We have a video on our youtube channel that shows this use case: https://www.youtube.com/watch?v=smrFuVLfiBc

smacdonald2008
smacdonald2008Accepted solution
New Participant
February 15, 2017

The outside Java app should only post to an AEM servlet.

So you need 2 parts: 

1 - outside client app (which you have) that posts files to AEM. 

2 - an AEM Sling servlet that uses the AssetManager API

See this article that does this exact use case from a outside Java Swing client:

  • handles multiple files by reading an XML file (defines where the assets are located, etc)
  • posts each file to a Sling servlet that handles the file and uses AssetManager

https://helpx.adobe.com/experience-manager/using/multiple-digital-assets.html

Use of the AssetManager API is the best way when you want to upload files from an outside client. 

New Participant
February 15, 2017

This will help only when my servlet inside OSGI container. But mine is standalone application. I am using it outside the AEM server. In the above link we are getting ResourceResolver from ResourceResolverFactory. In my case it will give null for ResourceResolver. 

For your info If I have tried this using curl command 

curl -u admin:admin -T D:\images\test.jpg http://localhost:4502/content/dam/test/test.jpg

It is working fine. I am getting metadata. But mine is java application.

smacdonald2008
New Participant
February 15, 2017

When coding the Servlet that accepts the file - use the AssetManager API. See this article -- 

https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html

Hope this helps...