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