如何在jsp中显示图片?

发布于 2024-10-20 18:23:16 字数 1144 浏览 2 评论 0原文

我有一个字节数组图像。

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年慕年 2024-10-27 18:23:16

您在 JSP 中生成的 HTML 必须包含一个 img 元素,该元素的 src 指向 servlet 或操作的 URL,该 servlet 或操作将从数据库加载图像并将其发送到具有 image/jpeg 内容类型的输出流。

// in your HTML :
<img src="/getImage.action?imageId=${id_of_the_image}"/>

// in the servlet mapped to /getImage.action:
// get the ID of the image from the request parameters
String imageId = request.getParameter("imageId");
byte[] imageData = getImageFromDatabase(imageId);
response.setContentType("image/jpeg");
response.getOutputStream().write(imageData);

所有浏览器都有右键单击 - 将图像另存为...菜单项,因此我不会在应用程序中实现此功能。

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.

// in your HTML :
<img src="/getImage.action?imageId=${id_of_the_image}"/>

// in the servlet mapped to /getImage.action:
// get the ID of the image from the request parameters
String imageId = request.getParameter("imageId");
byte[] imageData = getImageFromDatabase(imageId);
response.setContentType("image/jpeg");
response.getOutputStream().write(imageData);

All the browsers have a right-click - Save image as... menu item, so I wouldn't implement this in the app.

扬花落满肩 2024-10-27 18:23:16

JSP:

<div id="profileDiv" style="padding: 10px; border: solid 2px #D6D6D6;">
     <img src="imageDisplayProcess.do?pKey=<c:out value="${staff.staffId}" />"
                             width="117" height="160"
                             onError="loadImage()" onAbort="loadImage()" />
</div>

Servlet //imageDisplayProcess

imgByt = imageClass.getPhotograph();//return blob...
response.setContentType("image/jpg");
response.getOutputStream().write(imgByt);
response.getOutputStream().flush();
response.getOutputStream().close();

JSP:

<div id="profileDiv" style="padding: 10px; border: solid 2px #D6D6D6;">
     <img src="imageDisplayProcess.do?pKey=<c:out value="${staff.staffId}" />"
                             width="117" height="160"
                             onError="loadImage()" onAbort="loadImage()" />
</div>

Servlet //imageDisplayProcess

imgByt = imageClass.getPhotograph();//return blob...
response.setContentType("image/jpg");
response.getOutputStream().write(imgByt);
response.getOutputStream().flush();
response.getOutputStream().close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文