如何在jsp页面中查看多个图像?

时间:2014-07-03 04:43:10

标签: jsp struts2

我无法在jsp页面中查看多个图像,但该操作会从数据库中返回多个图像。如何在我的jsp中查看所有图像? 我的jsp页面result.jsp如下所示。

 <%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
   <head></head>
   <body>
     <h1>Struts 2 Dynamic Image Example</h1>
     <img src=" <s:url action='downloadAction' />" />
   </body>
</html>

动作类MultipleFileDownloadAction.class如下所示。

import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class MultipleFileDownloadAction extends ActionSupport implements SessionAware {

    private int id;
    private Map session;
    private String name;

    public String execute() throws Exception {
        getText();
        return "success";
    }
    private void getText() throws Exception {
        Blob b;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/afsaldb","root","applogic");
        PreparedStatement ps = con.prepareStatement("select img,name from multipleimg");

         ResultSet rs = ps.executeQuery();
            while (rs.next()) {


                    b = rs.getBlob(1);

                    setName(rs.getString(2));

                      HttpServletResponse response = ServletActionContext.getResponse();
                      response.reset();
                      response.addHeader("Content-Disposition", "attachment;filename=\""+name+"\"");
                      response.getOutputStream().write(b.getBytes(1, (int)b.length()));

                      response.getOutputStream().flush();
                      response.getOutputStream().close();

            }
          }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Map getSession() {
        return session;
    }
    public void setSession(Map session) {
        this.session = session;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

我的struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">


    <action name="downloadAction" 
         class="org.MultipleFileDownloadAction">
         <result name="success">result.jsp</result>
    </action>
   </package>
</struts>
数据库中的

表如下。 enter image description here 该程序的输出仅显示从数据库中检索的第一个图像。

请帮助

1 个答案:

答案 0 :(得分:1)

您可以在porject文件夹中创建图像,并可以在jsp中提供路径。

第1步:在项目文件夹中创建文件夹images

第2步:MultipleFileDownloadAction.java编辑以下代码

public class MultipleFileDownloadAction extends ActionSupport implements SessionAware {

private int id;
private Map session;
private String name;

private List<String> list;
//getter setters...
}

在执行方法或getText()方法中将代码更改为以下内容。

private void getText() throws Exception {
...
list = new ArrayList<String>();
while (rs.next()) {
byte[] bAvatar;
            try {
                if(rs.getBlob(1)!=null)
                {
       list.add(rs.getString(2));
                    bAvatar = rs.getBlob(1).getBytes(1, (int) rs.getBlob(1).length());
                    setName(rs.getString(2));
                 System.out.println(request.getRealPath("/")+"images\\"+rs.getString(2)+".jpg");
                    FileOutputStream fos = new FileOutputStream(request.getRealPath("/")+"images\\"+rs.getString(2)+".jpg"); 
                    fos.write(bAvatar);
                    fos.close();
                    bAvatar=null;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
  }
  }

这将在您的图像文件夹中创建图像。

第3步:在result.jsp文件中

<%@taglib uri="/struts-tags" prefix="s"%>
<table>
<s:iterator value="list" status="userStatus">
    <tr class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
        <td><s:property /></td>
        <td><img src="images/<s:property />.jpg" width="100" Height="100" alt="No image uploaded" /></td>
    </tr>
</s:iterator>
</table>

示例输出: Download Code here - Project URL

enter image description here