Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <%@ page import="java.sql.*" %>
  2. <%@ page import='java.io.InputStream' %>
  3. <%@ page import='java.io.OutputStream' %>
  4. <%
  5. String login = "root";
  6. String password = "";
  7. String url = "jdbc:mysql://localhost/dbimagenes";
  8. Connection conn = null;
  9. Statement statement = null;
  10. ResultSet rs = null;
  11. int nBytes = 0;
  12. %>
  13. <html><style type="text/css">
  14. <!--
  15. body {
  16. background-color: #F5f5f5;
  17. }
  18. -->
  19. </style><body>
  20. <h1>Imagen desde MySQL</h1>
  21. <table>
  22. <tr><td>
  23. <%
  24. Class.forName("com.mysql.jdbc.Driver").newInstance();
  25. conn = DriverManager.getConnection(url, login, password);
  26. statement = conn.createStatement();
  27. rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
  28. try {
  29. if (rs.next()) {
  30. response.setContentType("image/jpeg");
  31. InputStream is = rs.getBinaryStream(1);
  32. OutputStream aux = response.getOutputStream();
  33. out.println("jajaja");
  34.  
  35. byte[] buffer = new byte[4096];
  36. for (;;) {
  37. nBytes = is.read(buffer);
  38. if (nBytes == -1) {
  39. break;
  40. }
  41.  
  42. aux.write(buffer, 0, nBytes);
  43.  
  44. }
  45.  
  46. is.close();
  47. aux.flush();
  48. aux.close();
  49.  
  50. } else {
  51.  
  52. throw new SQLException("image not found");
  53. }
  54. rs.close();
  55. } catch (SQLException e) {
  56. out.println("Imagen no encontrada");
  57. }
  58.  
  59. out.println("no se muestra");
  60. %>
  61. </td></tr></table>
  62.  
  63. <p> Imagen</p>
  64. <a href="index.html">PRINCIPAL</a>
  65. </body>
  66. </html>
  67.  
  68. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  69. <%@include file="conexion.jsp" %>
  70. <!DOCTYPE html>
  71. <html>
  72. <head>
  73. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  74. <title>JSP Page</title>
  75. </head>
  76. <body>
  77. <h1>Insertar</h1>
  78. <%
  79. String nombre=request.getParameter("txtnombre");
  80. String marca=request.getParameter("txtmarca");
  81. String imagen=request.getParameter("txtimagen");
  82.  
  83. if(nombre!=null && marca!=null && imagen!=null){
  84. String qry ="insert into t_imagenes(nombre,marca,imagen) values('"+nombre+"','"+marca+"','"+imagen+"')";
  85. sql.executeUpdate(qry);
  86. out.print("Datos Registrados "
  87. + "<a href='index.jsp'>REGRESAR</a>");
  88.  
  89. }else{
  90.  
  91. %>
  92. <form name="frmimagenes" method="post" action="insertar.jsp">
  93. nombre: <input type="text" name="txtnombre"/><br/>
  94. marca: <input type="text" name="txtmarca"/><br/>
  95. imagen: <input type="file" name="txtimagen" value="" size="50" /><br/>
  96. <input type="submit" value="Guardar">
  97. </form>
  98.  
  99. <%}//else%>
  100. </body>
  101. </html>
  102.  
  103. OutputStream oImage;
  104. try {
  105. rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
  106. if(rs.next()) {
  107. byte barray[] = rs.getBytes(1);
  108. response.setContentType("image/gif");
  109. oImage=response.getOutputStream();
  110. oImage.write(barray);
  111. oImage.flush();
  112. oImage.close();
  113. }
  114. }
  115. catch(Exception ex){
  116. //ex.printStackTrace();
  117. }finally {
  118. try{
  119. if(con!=null)
  120. con.close();
  121. }catch(Exception ex){
  122. // ex.printStackTrace();
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement