Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- 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
- }
- 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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement