将图像路径显示为来自数据库JSP Struts的图像

时间:2014-07-28 08:53:04

标签: java image jsp struts2 filepath

我想从我的数据库中检索图像,数据类型是文件。但是,我只能找到blob或其他类型的解决方案,但不能找到filepath。所以,我的问题是如何使用filepath显示图像。

粗体文字是我感到困惑的。如何使用filepath实际显示图像。提前谢谢。

Display.java - >与retrieve.jsp链接

public String execute()
{

try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull","root","8899");
Statement st=con.createStatement();
ResultSet rs = st.executeQuery("select * from usertable");

    List li = null;
    li = new ArrayList();
    Mybean mb = null;

    while(rs.next())
        {
            mb = new Mybean();

            mb.setUserID(rs.getInt("userID"));
            **//mb.setUserImage(rs.get);** --> *In here, I am trying to setUserImage but I am not sure what is the correct way.*
            mb.setUserNRIC(rs.getString("userNRIC"));
            mb.setUserName(rs.getString("userName"));   
            mb.setUserEmail(rs.getString("userEmail"));
            mb.setUserPW(rs.getString("userPW"));   
            mb.setUserContact(rs.getInt("userContact"));
            mb.setUserAddress(rs.getString("userAddress")); 
            mb.setCatID(rs.getInt("catID"));
            mb.setCatTypeID(rs.getInt("catTypeID"));    
            mb.setSectionID(rs.getInt("sectionID"));
            mb.setStatus(rs.getString("status"));   

            li.add(mb);

        }

    request.setAttribute("disp", li);

    rs.close();
    st.close();
    con.close();

        }
    catch(Exception e){
        e.printStackTrace();
    }

        return SUCCESS;

}

retrieve.jsp - >显示图像

<%
List l=(List)request.getAttribute("disp");
if(l!=null)
{

Iterator it=l.iterator();

while(it.hasNext())
{

Student.Mybean b=(Student.Mybean)it.next();

int userID = b.getUserID();
**//File userImage = b.getUserImage();**
String userNRIC = b.getUserNRIC();
String userName = b.getUserName();
String userEmail = b.getUserEmail();
String userPW = b.getUserPW();
int userContact = b.getUserContact();
String userAddress = b.getUserAddress();
int catID = b.getCatID();
int catTypeID = b.getCatTypeID();
int sectionID = b.getSectionID();
String status = b.getStatus();

%>
<tr>
<td><input type="checkbox" value="<%= userID %>" name="rdel"></td>

<td><%= userID %></td>
**<td><img src="<s:url action='userImage' />" /></td>** 
<td><%= userNRIC %></td>
<td><%= userName %></td>
<td><%= userEmail %></td>
<td><%= userPW %></td>
<td><%= userContact %></td>
<td><%= userAddress %></td>
<td><%= catID %></td>
<td><%= catTypeID %></td>
<td><%= sectionID %></td>
<td><%= status %></td>

Save.java - &gt;我如何将文件路径保存到数据库

String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
File fileToCreate = new File(filePath, mb.userImageFileName);
FileUtils.copyFile(mb.userImage, fileToCreate);
ps.setString(1, mb.userImageFileName);

0 个答案:

没有答案