How to download the file in the CQ5 | Community
Skip to main content
October 16, 2015
Solved

How to download the file in the CQ5

  • October 16, 2015
  • 13 replies
  • 5785 views

We need to download the all extension files like pdf | txt | word |html and son on but except image in the CQ5, then to translate these files in localhost. At last upload these files back to CQ5.

The question is how to know the files list path and how to get the files stored in the CQ5 to localhost or a specific server and how to upload the file to CQ5?

 

Awaiting for anyone suggestion, thanks in advance!  

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

To download files, (for example, DAM Assets), you use the QueryBuilder API. Here is a community article that talks about how to build a custom Sling Servlet that downloads DAM Assets. In this use case - the collection of DAM Assets retrieved using the QueryBuilder API is placed in a ZIP file and downloaded, as shown in this pic:

[img]downloadClient.png[/img]

To read this development article, see: http://helpx.adobe.com/experience-manager/using/downloading-dam-assets.html.

13 replies

Sham_HC
Level 10
October 16, 2015

Hi York Jin,

   Typically all assets are stored in dam and referenced in pages. Though there is exception you can store at page level. You can write an jcr query to get assets of type you need and iterate over it to get files.

Thanks,

Sham

smacdonald2008
Level 10
October 16, 2015

Another way to download a DAM asset from CQ to the desktop is write a custom Sling Servlet and in the GET method search for the file using CQ APIS ( ie - QueryBuilder) - and then write logic in the Slign Servlet to download the file:

public class DownloadFileServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet{

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // reads input file from an absolute path
        //USE QUERY BUILDER TO GET FILE";
        FileInputStream inStream = new FileInputStream(downloadFile);
         
        // if you want to use a relative path to context root:
        String relativePath = getServletContext().getRealPath("");
        System.out.println("relativePath = " + relativePath);
         
        // obtains ServletContext
        ServletContext context = getServletContext();
         
        // gets MIME type of the file
        String mimeType = context.getMimeType(filePath);
        if (mimeType == null) {        
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        }
        System.out.println("MIME type: " + mimeType);
         
        // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
         
        // forces download
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
        response.setHeader(headerKey, headerValue);
         
        // obtains response's output stream
        OutputStream outStream = response.getOutputStream();
         
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
         
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
         
        inStream.close();
        outStream.close();     
    }
}

 
Peter_Puzanovs
Community Advisor
Community Advisor
October 16, 2015

Hi York,

More information on your use case would be nice,

(assume that you want to store files in DAM)

Here's some general code that will fulfill your task:

/**      * Downloads file from internet and places it in DAM      * @param url - the url of the file we are interested in      * @param fileName - the filename of the file we are interested to store      */     public void downloadAndStore(String url, String fileName) {         try {             final URL file = new URL(url);             final ReadableByteChannel readableByteChannel = Channels.newChannel(file.openStream());             final FileOutputStream fos = new FileOutputStream("temp"); // ToDo make sure to have a unique name in case of concurrency             fos.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);             final ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);             final InputStream is = ..;             IOUtils.copy(is, fos);             //Use AssetManager to place the file into the AEM DAM             final AssetManager assetMgr = resourceResolver.adaptTo(AssetManager.class);             final String fileToStore = "/content/dam/" + fileName;             assetMgr.createAsset(fileToStore, fos, "image/jpeg", true);             // ToDo delete temp file         } catch (IOException e) {             //ToDo log at appropriate level         }     }

Kindest Regards,

Peter