Advertisement
Guest User

Untitled

a guest
Jun 18th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. private static final String HOST = "200.45.175.34";
  2. private static final String PORT = 5896;
  3. private static final String DATABASE = "bdremota";
  4. private static final String URL = "jdbc:mysql://" + HOST
  5. + ":" + PORT
  6. + "/" + DATABASE;
  7.  
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver");
  10. Connection conn = DriverManager.getConnection(URL);
  11. } catch(ClassNotFoundException e) {
  12. // hacer algo
  13. }
  14.  
  15. public Optional<ImageIcon> readImage(uid) {
  16. String sql = "SELECT bin_data FROM imagenes WHERE uid = ?";
  17. try (Connection conn = ConnectionHelper.getConnection();
  18. PreparedStatement pst = conn.prepareStatement(sql) {
  19. pst.setString(1, uid); // el uid de la imagen a leer
  20. ResultSet rs = pst.executeQuery();
  21. ImageIcon icon = null;
  22. if(rs.next()) {
  23. byte[] image = IOUtils.toByteArray(rs.getBinaryStream());
  24. icon = new ImageIcon(image);
  25. }
  26. return Optinal.of(icon);
  27. } catch(SQLException e) {
  28. // hacer algo
  29. return Optional.empty();
  30. }
  31. }
  32.  
  33. Optional<ImageIcon> oIcon = Clase.readImage("10383");
  34. // si la imagen existe
  35. if(oIcon.isPresent()) {
  36. // mostrarla en el JPanel
  37. }
  38.  
  39. clientSocket = new Socket("localhost", 6002);
  40. outputStream = clientSocket.getOutputStream();
  41. dataOutputStream = new DataOutputStream(outputStream);
  42. inputStream = clientSocket.getInputStream();
  43.  
  44. // mandamos el uid
  45. dataOutputStream.writeUTF("1458");
  46.  
  47. // y el servidor nos devuelve la imagen
  48. byte[] bufferSize = new byte[4096];
  49. inputStream.read(bufferSize);
  50. int size = ByteBuffer.wrap(buffeSize).asIntBuffer().get();
  51. byte[] buffer = new byte[size];
  52. inputStream.read(buffer);
  53.  
  54. // convertimos el InputStream a byte[] para pasarlo a ImageIcon
  55. byte[] binaryImage = new ByteArrayInputStream(buffer).toByteArray();
  56. return new ImageIcon(binaryImage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement