Advertisement
Guest User

Untitled

a guest
Aug 16th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>your Pic</title>
  8. </head>
  9. <body>
  10.  
  11. <form id="form2" enctype="multipart/form-data" action="getPhoto.jsp" method="get">
  12. <table>
  13. <tr>
  14. <td>Enter Photo Id :</td>
  15. <td><input type="text" name="id"/></td>
  16. </tr>
  17.  
  18. </table>
  19. <p/>
  20. <input type="submit" value="fetch Photo"/>
  21. </form>
  22.  
  23. </body>
  24. </html>
  25.  
  26. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  27. pageEncoding="ISO-8859-1"%>
  28. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  29. <html>
  30. <head>
  31. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  32. <title>Your photo</title>
  33. </head>
  34. <body>
  35. <% String p_id=request.getParameter("id");%>
  36.  
  37.  
  38. <table>
  39. <tr><td><%=p_id%></td></tr>
  40. <tr><td><img src="/getPic?photoid=<%=p_id%>" /></td></tr>
  41. </table>
  42. </body>
  43. </html>
  44.  
  45. package com.kp.imagehandler;
  46.  
  47. import java.io.BufferedInputStream;
  48. import java.io.BufferedOutputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51.  
  52. import javax.servlet.ServletException;
  53. import javax.servlet.http.HttpServlet;
  54. import javax.servlet.http.HttpServletRequest;
  55. import javax.servlet.http.HttpServletResponse;
  56. import com.kp.imagehandler.Image;
  57.  
  58. /**
  59. * Servlet implementation class getPic
  60. */
  61. public class getPic extends HttpServlet {
  62. private static final long serialVersionUID = 1L;
  63.  
  64. /**
  65. * @see HttpServlet#HttpServlet()
  66. */
  67. public getPic() {
  68. super();
  69. // TODO Auto-generated constructor stub
  70. }
  71.  
  72. /**
  73. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  74. */
  75. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  76. // TODO Auto-generated method stub
  77.  
  78. String imageId = request.getParameter("photoid");
  79. System.out.println(imageId);
  80. InputStream photoStream = (new Image()).getImageStream(imageId);
  81.  
  82.  
  83. BufferedInputStream input = null;
  84. BufferedOutputStream output = null;
  85.  
  86. try {
  87.  
  88. // Open streams
  89. input = new BufferedInputStream(photoStream, 200000);
  90. output = new BufferedOutputStream(response.getOutputStream(),
  91. 200000);
  92.  
  93. // Write file contents to response.
  94. byte[] buffer = new byte[200000];
  95. int length;
  96. while ((length = input.read(buffer)) > 0) {
  97. output.write(buffer, 0, length);
  98. }
  99.  
  100. } finally {
  101. output.close();
  102. input.close();
  103. }
  104.  
  105. }
  106.  
  107. /**
  108. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  109. */
  110. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  111. // TODO Auto-generated method stub
  112. }
  113.  
  114. }
  115.  
  116. package com.kp.imagehandler;
  117.  
  118. import java.io.InputStream;
  119. import java.sql.Connection;
  120. import java.sql.DriverManager;
  121. import java.sql.ResultSet;
  122. import java.sql.SQLException;
  123. import java.sql.Statement;
  124.  
  125. public class Image {
  126.  
  127. // Init ---------------------------------------------------------------------------------------
  128.  
  129.  
  130. public InputStream getImageStream(String pid)
  131. {
  132. InputStream IS=null;
  133. try {
  134.  
  135. Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@10.75.122.69:1521:devdb",
  136. "s3", "s3");
  137. Statement stmt = connection.createStatement ();
  138.  
  139. ResultSet rset = stmt.executeQuery ("SELECT photo FROM photo_holder where id="+pid);
  140.  
  141. while(rset.next())
  142. {
  143. IS=rset.getBinaryStream("photo");
  144.  
  145. }
  146. } catch (SQLException e) {
  147. // TODO Auto-generated catch block
  148. e.printStackTrace();
  149. }
  150.  
  151.  
  152. return IS;
  153.  
  154. }
  155.  
  156. // Implement default getters and setters here.
  157.  
  158. }
  159.  
  160. <servlet>
  161. <description>getPic</description>
  162. <display-name>getPic</display-name>
  163. <servlet-name>getPic</servlet-name>
  164. <servlet-class>com.kp.imagehandler.getPic</servlet-class>
  165. </servlet>
  166. <servlet-mapping>
  167. <servlet-name>getPic</servlet-name>
  168. <url-pattern>/getPic</url-pattern>
  169. </servlet-mapping>
  170.  
  171. create table PHOTO_HOLDER
  172. (
  173. ID NUMBER(5) not null,
  174. TITLE VARCHAR2(50),
  175. PHOTO BLOB
  176. )
  177.  
  178. <form id="form2" enctype="multipart/form-data" action="<%=request.getContextPath() %>/getPhoto.jsp" method="get">
  179.  
  180. <img src="<%=request.getContextPath() %>/getPic?photoid=<%=p_id%>" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement