如何在jsp中显示图片?
我有一个字节数组图像。
我需要在 jsp 页面中以 jpg 格式显示该图像,单击图像时,我可以将图像下载到我的电脑:
我正在从 mysql 数据库将图像作为字节数组加载。
我的代码是
ResultSet res = statement.executeQuery("SELECT * FROM
upload_data where user_id = "+userID);
while (res.next()) {
contactDetails = new ContactDetails();
contactDetails.setContactPhoto(res.getBytes("photo"));
byteArrayBackToImage1(res.getBytes("photo"));
contactsList.add(contactDetails);
}
public void byteArrayBackToImage1(byte[] imageInByte){
try{
Random rand = new Random();
int numNoRange = rand.nextInt();
String number = String.valueOf(numNoRange);
//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
System.out.println("bImageFromConvert : "+bImageFromConvert);
/*ImageIO.write(bImageFromConvert, "jpg",
new File("c:\\"+number+".jpg")); */
}catch (Exception e) {
// TODO: handle exception
}
我需要显示图像在jsp中,
例如:image.jpg image2.jpg
并通过单击 image.jsp ,我可以下载该图像并将其保存到我的电脑上
请帮忙
I have a bytearray image.
I need to show that image in jpg format in jsp page and while clicking the image, i can download the image to my pc:
I am loading the image from my mysql db as byte array..
My code is
ResultSet res = statement.executeQuery("SELECT * FROM
upload_data where user_id = "+userID);
while (res.next()) {
contactDetails = new ContactDetails();
contactDetails.setContactPhoto(res.getBytes("photo"));
byteArrayBackToImage1(res.getBytes("photo"));
contactsList.add(contactDetails);
}
public void byteArrayBackToImage1(byte[] imageInByte){
try{
Random rand = new Random();
int numNoRange = rand.nextInt();
String number = String.valueOf(numNoRange);
//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
System.out.println("bImageFromConvert : "+bImageFromConvert);
/*ImageIO.write(bImageFromConvert, "jpg",
new File("c:\\"+number+".jpg")); */
}catch (Exception e) {
// TODO: handle exception
}
I need to show the image in jsp as
eg: image.jpg
image2.jpg
and by clicking image.jsp , i can download that image and save it to my pc
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在 JSP 中生成的 HTML 必须包含一个 img 元素,该元素的 src 指向 servlet 或操作的 URL,该 servlet 或操作将从数据库加载图像并将其发送到具有 image/jpeg 内容类型的输出流。
所有浏览器都有右键单击 - 将图像另存为...菜单项,因此我不会在应用程序中实现此功能。
The HTML you generate in your JSP must contain an img element with an src pointing to the URL of a servlet or action which will load the image from the database and send it to the ouput stream with the image/jpeg content type.
All the browsers have a right-click - Save image as... menu item, so I wouldn't implement this in the app.
JSP:
Servlet //imageDisplayProcess
JSP:
Servlet //imageDisplayProcess