| |

VerySource

 Forgot password?
 Register
Search
View: 798|Reply: 7

anxious! Problems reading pictures from a database and outputting them to a JSP page

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-9 13:30:02
| Show all posts |Read mode
...

try {

ResultSet rs = s.executeQuery ("select * from pictures");
response.setContentType ("image / jpeg");
OutputStream os = response.getOutputStream ();
out.write ("<table> <tr> <td> picture </ td> </ tr>");
while (rs.next ())
{

byte [] dt = rs.getBytes (2);
os.write (dt);

}
os.flush ();
rs.close ();

} catch (Exception e)
     {
         e.printStackTrace ();
         throw new SQLException ("Error:" + e);
     }
...
In SQL Server, there is a table named “pitures”, where the second field is of type “image”, there are three rows in the table, and the second field of each row is a .JPG image.
My question is: First, every time I read the data from the table using the above code and display it in the JSP page, only the picture in the first row is always displayed. No exception was thrown. Second, out.write () cannot output content to the JSP page.
Seeking a solution.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-30 19:45:02
| Show all posts
Follow the program
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-31 01:15:01
| Show all posts
Put the TR in out.write ("<table> <tr> <td> picture </ td> </ tr>"); into the while loop to see
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-5-31 22:45:02
| Show all posts
try {

ResultSet rs = s.executeQuery ("select * from pictures");
response.setContentType ("image / jpeg");
OutputStream os = response.getOutputStream ();
out.write ("Test picture output");
while (rs.next ())
{

byte [] dt = rs.getBytes (2);
os.write (dt);

}
os.flush ();
rs.close ();

} catch (Exception e)
     {
         e.printStackTrace ();
         throw new SQLException ("Error:" + e);
     }

The problem is still the same
Reply

Use magic Report

0

Threads

21

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 United States

Post time: 2020-6-2 09:00:02
| Show all posts
Can multiple images be returned in one HTTP request?
Personally think it should be impossible, at least not so simple.
response.setContentType("image/jpeg");
This is a comment on a base64 encoded segment. How can a segment be mixed in a segment at the same time, as well as multiple images?

Therefore, multiple pictures should be given the access address in a page, which is accessed one by one by the browser and get separately
Your program can accept a parameter to determine which picture is being accessed to give a response.
Reply

Use magic Report

0

Threads

14

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-6-2 14:15:01
| Show all posts
First read it out and store it in a temporary folder, then load the picture, isn't it better?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-5 09:00:01
| Show all posts
It won't work like this.

Because you set the file header to be a picture type. Then it will display the entire returned content as the content of a picture  
response.setContentType("image/jpeg");


This is a secondary response problem. .

You should first output the code of the entire page <html>.

Connect to the servlet where you want to display the picture
<html>
  <table>
    <tr>
      <td>
         <img scr="Servlet connection..............." ><!--Picture 1 connection-->
      </td>
      <td>
         <img scr="Servlet connection..............." ><!--Picture 2 connection-->
      </td>
      <td>
         <img scr="Servlet connection..........." ><!--Picture 3 connection-->
      </td>
   </tr>
  </table>
<html>
Reply

Use magic Report

0

Threads

21

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-6-25 01:30:01
| Show all posts
sundaydaysaid what I meant
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list