View Images saved on a remote server in a Java client

0

I have to create a Java application with a remote client-server graphical interface, which allows the client to view an image stored on a remote server (my pc), I do not want to save the image, just visualize it from a JPanel or something similar.

Is this possible?

    
asked by Bryan Romero 17.06.2016 в 16:57
source

2 answers

2

Right now I imagine three options:

  • Have a database on the remote server that stores the images.
  • Read images through socket.
  • Have the images in a CDN.
  • Remote database

    The way to connect is as if you were doing it locally. Suppose your IP is 200.45.175.34 and port 5896 .

    private static final String HOST = "200.45.175.34";
    private static final String PORT = 5896;
    private static final String DATABASE = "bdremota";
    private static final String URL = "jdbc:mysql://" + HOST 
                                        + ":" + PORT 
                                        + "/" + DATABASE;
    
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(URL);
    } catch(ClassNotFoundException e) {
        // hacer algo
    }
    

    And to obtain a certain image you can do it by means of the UID of the image:

    public Optional<ImageIcon> readImage(uid) {
        String sql = "SELECT bin_data FROM imagenes WHERE uid = ?";
        try (Connection conn = ConnectionHelper.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql) {
            pst.setString(1, uid); // el uid de la imagen a leer
            ResultSet rs = pst.executeQuery();
            ImageIcon icon = null;
            if(rs.next()) {
                byte[] image = IOUtils.toByteArray(rs.getBinaryStream());
                icon = new ImageIcon(image);
            }
            return Optinal.of(icon);
        } catch(SQLException e) {
            // hacer algo
            return Optional.empty();
        }
    }
    
    Optional<ImageIcon> oIcon = Clase.readImage("10383");
    // si la imagen existe
    if(oIcon.isPresent()) {
        // mostrarla en el JPanel
    }
    

    By means of Socket

    clientSocket = new Socket("localhost", 6002);
    outputStream = clientSocket.getOutputStream();
    dataOutputStream = new DataOutputStream(outputStream);
    inputStream = clientSocket.getInputStream();
    
    // mandamos el uid
    dataOutputStream.writeUTF("1458");
    
    // y el servidor nos devuelve la imagen
    byte[] bufferSize = new byte[4096];
    inputStream.read(bufferSize);
    int size = ByteBuffer.wrap(buffeSize).asIntBuffer().get();
    byte[] buffer = new byte[size];
    inputStream.read(buffer);
    
    // convertimos el InputStream a byte[] para pasarlo a ImageIcon
    byte[] binaryImage = new ByteArrayInputStream(buffer).toByteArray();
    return new ImageIcon(binaryImage);
    

    By means of CDN

    Look for a CDN image server that has a simple API and use it. One option is Cloudinary

        
    answered by 18.06.2016 / 15:02
    source
    0

    Short answer: Yes

    Long answer: Some time ago I had to do something similar.

    The only "stable" solution I found was to make a java image server (it was hosted on my PC or on the PC you wanted), which was responsible for reading requests from a port and sending the images they asked for.

    That is, the client connected and asked for the list of images, the server gave him the list of images he had and the client from there asked him for images.

    This worked on a local network, with the Internet in the middle, I'm not sure if it would be good.

    If you have to go online, I think it would be a better option to upload the images to Picasa or imgur or a similar server and download them from the application you make.

    Surely the option that I took now is not the best, but it worked for me.

        
    answered by 18.06.2016 в 10:59