I want to display images in mysql with the path.In my project i had done the copy of image in my project.I took the copied path too.I want to display the image with that path.I am getting the path in jsp but the images doesn't displaying .ANYONE PLEASE HELP ME
Form.jsp
<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="UploadFile.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
<table>
</center>
</FORM>
</HTML>
UploadFile.jsp
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("D:\\REAL\\RoseIndia\\images\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
System.out.println("THE FILE OUT PATH ARE:"+fileOut);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>
<FORM ENCTYPE="multipart/form-data" ACTION="DisplayImage" METHOD=POST>
<input type="submit" value="Send File">
</form>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/realtime";
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
ps = connection.prepareStatement("insert into file(image) values(?)");
System.out.println("THE FF PATH ARE:"+ff.getPath());
ps.setString(1, ff.getPath());
int s = ps.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}
}
catch(Exception e){
e.printStackTrace();
}
}
%>
DisplayImage.java
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DisplayImage")
public class DisplayImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "realtime";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
Statement st;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected!");
String Name = request.getParameter("Name");
ArrayList al = null;
ArrayList Name_list = new ArrayList();
String query = "select * from file ";
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
al = new ArrayList();
al.add(rs.getString(1));
System.out.println("al :: " + al);
Name_list.add(al);
}
// out.println("'"+al+"'");
request.setAttribute("piList", Name_list);
RequestDispatcher view = request.getRequestDispatcher("ViewUsers.jsp");
view.forward(request, response);
conn.close();
System.out.println("Disconnected!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
In this file iam getting blank images & the path of that images are correct
ViewUsers.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table width="1200px" align="center" enctype="multipart/form-data"
style="border:1px solid #000000;">
<tr>
<td colspan=5 align="center"
style="background-color:teal">
<b></b></td>
</tr>
<tr style="background-color:lightgrey;">
<td><b>IMAGE</b></td>
</tr>
<%
int count = 0;
String color = "#F9EBB3";
if (request.getAttribute("piList") != null) {
ArrayList al = (ArrayList) request.getAttribute("piList");
System.out.println("Hai this is from ViewUsers.jsp"+al);
Iterator itr = al.iterator();
while (itr.hasNext()) {
if ((count % 2) == 0) {
color = "#eeffee";
}
count++;
ArrayList pList = (ArrayList) itr.next();
System.out.println("Hai this is from ViewUsers WHILE LOOP"+pList);
%>
<tr style="background-color:<%=color%>;">
<td><%=pList.get(0)%></td>
<%System.out.println("Hai this is from ViewUsers WHILE LOOP"+pList.get(0));%>
<img src="<%=pList.get(0)%>" alt="Image" width="160" height="160" class="img-thumbnail">
</tr>
<%
}
}
if (count == 0) {
%>
<tr>
<td colspan=4 align="center"
style="background-color:#eeffee"><b>Nope</b></td>
</tr>
<% }
%>
</table>
</body>
</html>