Skip to main content

Featured

Adobe Experience Manager - Create an OSGI Configuration

 In this article, let's create an OSGi configuration, configure it and use it in AEM code. So now let's get started with the creation of an OSGi configuration. Technical details and Assumptions: All the following details are tested in AEM version 6.5.8, Java version 11.0.2 Creation of OSGi configuration: To create an OSGi configuration we need to create an ObjectClassDefinition. I have included a sample OCD configuration, which can be used as a reference to create one. The next step would be to create an interface and an implementation that can help fetch the OSGi configurations.  Interface: Implementation: Let's try to use the OSGi configuration created so far in Models/Servlets. For demonstration purposes, I used AEM Models here, but the same can be implemented in Servlets too. Now that we have created the OSGi configuration. Once building the code, we should be able to see the OSGi configuration in the web console (http://localhost:4502/system/console/configMgr) C...

Implementation of HTTP socket for File Upload and Download

In the below code the client uploads the image to the server, and for downloading the code is just the inverse and the code is executed as vice versa.
Here the image is transfered for instance the transfering file can be of any format based on it the code varies a little bit.

 Server.java:

 import java.net.*;
import java.io.*;
import java.awt.image.*;
 import javax.imageio.*;
import javax.swing.*;
class Server {
public static void main(String args[]) throws Exception{
    ServerSocket server=null;
    Socket socket;
    server=new ServerSocket(4000);
    System.out.println("Server Waiting for image");
    socket=server.accept(); System.out.println("Client connected.");
    InputStream in =    socket.getInputStream();
    DataInputStream dis = new DataInputStream(in);
    int len = dis.readInt();
    System.out.println("Image Size: " + len/1024 + "KB");
    byte[] data = new byte[len];
    dis.readFully(data);
    dis.close();
    in.close();
    InputStream ian = new ByteArrayInputStream(data);
    BufferedImage bImage = ImageIO.read(ian);
    File outputfile = new File("bicycle1.png");
    ImageIO.write(bImage, "png", outputfile);
}
}

Client.java:

import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Client{
public static void main(String args[]) throws Exception{
    Socket soc;
    BufferedImage img = null;
    soc=new Socket("localhost",4000);
    System.out.println("Client is running. ");
     try {
        System.out.println("Reading image from disk. ");
        img = ImageIO.read(new File("bicycle.jpg"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", baos);
        baos.flush();
        byte[]  bytes = baos.toByteArray();
        baos.close();
        System.out.println("Sending image  ");
        OutputStream out = soc.getOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        dos.writeInt(bytes.length);
        dos.write(bytes, 0, bytes.length);
        System.out.println("Image sent ");
        dos.close();
        out.close();
    }catch (Exception e) {
        System.out.println("Exception: " + e.getMessage());
        soc.close();
    }
    soc.close();
}
}

Output:

Comments

Popular Posts