2013-10-17 2 views
0

java를 사용하여 mysql 데이터베이스에서 blob 이미지를 검색하려고합니다. 이제 문제는 한 번에 하나의 이미지 만 가져올 수 있으며 다른 이미지는 표시되지 않는다는 것입니다.jsp를 사용하여 mysql에서 여러 blob 이미지를 검색하는 방법

다음 내 JSP 코드는 내가이 일을하고있어 곳이다 (단지 데모 목적을 위해) :

<?xml version="1.0" encoding="UTF-8" ?> 
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 
    <%@ page language="java" %> 
    <%@ page import="java.sql.*" %> 
    <%@ page import="java.io.*" %> 
    <%@ page import="java.util.*"%> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
    <title>MindDotEditor posted Data</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta name="robots" content="noindex, nofollow" /> 
    <link href="../sample.css" rel="stylesheet" type="text/css" /> 
    <link rel="shortcut icon" href="../fckeditor.gif" type="image/x-icon" /> 
    </head> 
    <body> 

     <% 
String url = "jdbc:mysql://localhost:3306/grandsho_register"; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 


try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    con = DriverManager.getConnection(url,"root","root"); 
    stmt = con.createStatement(); 
    rs = stmt.executeQuery("SELECT image FROM user "); 
    int i = 1; 
    if(rs.next()) { 
     Blob len1 = rs.getBlob("image"); 
     int len = (int)len1.length(); 
     byte[] b = new byte[len]; 
     InputStream readImg = rs.getBinaryStream(1); 
     int index = readImg.read(b, 0, len); 
     System.out.println("index" +index); 
     stmt.close(); 
     response.reset(); 
     response.setContentType("image/jpg"); 
     response.getOutputStream().write(b,0,len); 
     response.getOutputStream().flush(); 
    } 
} catch(Exception ex) { 
    out.println(ex); 
} finally { 
    rs.close(); 
    stmt.close(); 
    con.close(); 
} 
     %> 

    <br> 
    <center><input type="button" value="Print" onclick="window.print();return false;" />  </center> 
    </body> 
    </html>  

사람이 내가 JSP 페이지에 여러 이미지를 표시 할 수있는 방법을 제안 할 수 있습니다? 증오에서 당신을 통해 여러 개의 이미지를 검색하는 것을 알고 싶어

1.first 일을 코딩 :

+0

@ShreyosAdikari 내가 변경해야 할 곳을 알려주시겠습니까? 특히 결과가 표시 될 때 특히 그렇습니다. 더 일찍 링크를 통과했다. – puneetjava

답변

0

대신 if(rs.next()) {} 당신은 내가 유에게 솔루션을 제공합니다 귀하의 모든 질문에 대한 while(rs.next()){}

-1

안녕들을 사용해야합니다 쿼리 (사진에서 이미지 선택)와 같은 쿼리를 사용할 수 없습니다. 한 번에 하나의 이미지 만 검색 할 수 있지만 여러 가지 방법으로 여러 이미지를 검색 할 수 있습니다.

[* -> 내가 더 많은 사람들이 여기에 DB에 이미지를 업로드했으면 좋겠다. 이미지 업로드를위한 코딩을 원한다면 여러 이미지를 검색하기 만하겠다.

2.for 이미지 작업은 먼저

---> 평민 - 파일 업로드-1.3.jar http://www.java2s.com/Code/Jar/c/Downloadcommonsfileupload13jar.htm

---> 공유지-IO-2.4 귀하의 프로젝트 Libaries에이 jar 파일을 추가 할 필요가 .jar http://www.java2s.com/Code/Jar/c/Downloadcommonsio24jar.htm

3.create SQL 쿼리를 사용하여 테이블 (내가 MySQL을 사용하고있다)

테이블 upload_image (아이디 INT NOT NULL AUTO_INCREMENT 기본 키, bImage의 Blob)을 생성;

4.Once이 병을 가져온 테이블을, 어떤 나쁜 일 이잖아 코딩을 할 코드를 추가하는 그것의 시간을 만들어

최초의 HTML 코드 : < ----- index.jsp를 ----- ->

`

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Reterving multiple images</title> 
</head> 

<body> 
    <img src="veiw.jsp?id=1"> 
    <img src="veiw.jsp?id=2"> 
</body> 

</html> 

`

< ----- veiw.jsp ------>

[<%@ page import="java.sql.*" %> 
<%@ page import="org.apache.commons.fileupload.*"%> 
<%@ page import="org.apache.commons.io.output.*"%> 
<%@ page import="org.apache.commons.fileupload.servlet.*"%> 
<%@ page import="org.apache.commons.fileupload.disk.*"%> 
<%@ page import="java.io.*"%> 
<%@ page import="java.util.*"%> 
<% 
String url="jdbc:mysql://localhost:3306/testzeroDateTimeBehavior=convertToNull"; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 
String j=request.getParameter("id"); 
int i=Integer.parseInt(j); 
try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    con = DriverManager.getConnection(url,"root",""); 
    stmt = con.createStatement(); 
    rs = stmt.executeQuery("SELECT bImage FROM upload_image where id="+i); 
     OutputStream o = response.getOutputStream(); 
    if(rs.next()) 
    { 
      Blob bl = rs.getBlob(1); 
      byte\[\] pict = bl.getBytes(1,(int)bl.length()); 
      response.setContentType("image/jpg"); 
      o.write(pict); 
      o.flush(); 
      o.close(); 


    } 

} 
catch(Exception ex) 
{ 
    out.println(ex); 
} 
finally 
{ 
    rs.close(); 
    stmt.close(); 
    con.close(); 
} 

%>[image shows the added jar file then start processing from step 3 ][3] 
+0

여기에 2 개의 이미지가있는 데모가 이미 db에 있습니다. – viswarajramji

관련 문제