Honour Cooker

Greenhorn
+ Follow
since Aug 16, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Honour Cooker

Hi,


1> What interface are you expecting when clicking an image or a text
area? Is it a popup window that will have image or text list? or you
want to display just list box?

I prefer a popup window comes up to show the actural images or different text in different textareas (user can directly see the content) depending on an image area or a text area is clicked.

2> How many images would be there in list box. Do you considering buffering
(preloading) of those images or want to source it from server when
selecetd?

As many as possible, but it might be not realistic to show 100 images in a popup. The client would like to drag the image to the template area??? Any ideas??? I have a mysql database running on ther server. The images and text are saved as blob. The data list is selected via sql statememnt.

3> How the image area would look like?

It is a blank jpeg image with a color background, embedded in the template table. I think there is a onclicke event of that image. When I click the image area, the popup comes up.

4> What the text content would be?
Ex. if you select "text1" from list, from where the corresponding
content would come? Would the content be bigger in size or just a few
lines.v

Text could be a MS doc and is from the database as images. This project is for a newspaper. I want the textarea show all the content of the text. If it is too big. It is ok to just show the description of the text in the list.

I did some research about it. The popup window could be an iframe, but for large images and documents, iframe might have some limitation.

Thank you very much for helping.

Honour
20 years ago
JSP
Hi Julian,

I have changed the code as below and it worked!!!
-------------------------- saving image to database -----------------------
private void savePayload(String filename,BufferedInputStream is) throws java.io.IOException{
int c;
PushbackInputStream input = new PushbackInputStream(is,128);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
while ( (c=read(input,boundary)) >= 0 )blob.write( c );
saveBlob(filename,blob.toByteArray());
blob.close();
}

private void saveBlob(String filename,byte[] out){
Connection con = null;
PreparedStatement pstmt = null;
String sqlCmd = "INSERT INTO tblContent (FileName,BinaryData) VALUES(?,?)";
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbUrl);

pstmt = con.prepareStatement(sqlCmd);
pstmt.setString(1, filename);
pstmt.setBytes(2, out);
pstmt.executeUpdate();

con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}

--------------------retrieving from database ---------------------------
public byte[] getBlob(int id){
String sqlQuery = "SELECT BINARYDATA FROM tblContent WHERE ContentID = ?;";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Blob blob = null;
byte[] bytes = null;
String description = "";
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbUrl);
pstmt = con.prepareStatement(sqlQuery);
pstmt.setInt(1,id);

rs = pstmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();
while (rs.next()) {
blob = rs.getBlob(1);
}
bytes = blob.getBytes( 1, (int)(blob.length()));
con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
return bytes;
}
-----------------------------------------------------------------
You are right. I did not save image properly. Thank you very much!

Honour
Hello everyone,

I am implementing a web based content management system. Basically I want to
have a jsp blank template page containing some blank text area and image area. When clicking an image area or a text area, there is an image or text list showing up for select. When user chooses one of the content from the list, the jsp template page will automatically show the content in the area clicked.

I am new to jsp and wonder if my idea is possible to implement. If it is ok, what technology do I need to make a start or any suggestions?

Thank you very much!
20 years ago
JSP
Thanks. The problem is still not fixed. I could see the binary data saved in table via phpAdmin. The saved in and retrieved out binary length are the same. I am going to take another way to save the file path to database instead.
20 years ago
JSP
Thanks for replying.

What I mean was the image data seemed saved in the database as I could see the binary data in the table field via phpAdmin. But I tried to implement a servlet to save the binary data to an image file, this file wont be opened by photoshop. So the data might not be save into database correctly.
Hi everyone,

I have a upload.jsp page which saves image into database blob. It seems working "fine" (no error message) when I click upload button after choosing a file from the disk. But I could not retrieve the image saved in the database. I suspect that I did not save the image correctly into database. The following is the jsp file saving image into database. Please help me!
-------------------------------------------------------
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<%
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;
}

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Driver Loaded!");
} catch (Exception E) {
out.println("Unable to load driver.");
}

try {
String connectionURL = "jdbc:mysql://localhost:3306/meg?user=;password=";
Connection C = DriverManager.getConnection(connectionURL,"","");
C.setAutoCommit(false);

String insert = "insert into tblContent (content) values (?)";

PreparedStatement pstmt = C.prepareStatement(insert);
pstmt.setObject(1, dataBytes);
pstmt.executeUpdate();

pstmt.close();
C.close();
out.println("Inserted row " );

} catch (SQLException E) {
out.print( "SQLException: " + E.getMessage());
} catch (Exception E) {
out.print("Error " + E.toString());
}



}
%>
-----------------------------------------------------
Thanks!
This is upload.jsp I use to save image to mysql database. I am using com.mysql.jdbc.Driver

----------------------------------------------------
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<%
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;
}

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Driver Loaded!");
} catch (Exception E) {
out.println("Unable to load driver.");
}

try {
String connectionURL = "jdbc:mysql://localhost:3306/meg?user=;password=";
Connection C = DriverManager.getConnection(connectionURL,"","");
C.setAutoCommit(false);

String insert = "insert into tblContent (content) values (?)";

PreparedStatement pstmt = C.prepareStatement(insert);
pstmt.setObject(1, dataBytes);
pstmt.executeUpdate();

pstmt.close();
C.close();
out.println("Inserted row " );

} catch (SQLException E) {
out.print( "SQLException: " + E.getMessage());
} catch (Exception E) {
out.print("Error " + E.toString());
}



}
%>

-------------------------------------------------------
Thanks!
20 years ago
JSP
Hi Julian,

Thank you very much for help. I have replaced your sample code with my database code. It worked good as gold.

My problem is to extract image blob from database not a local file. Inspired by you, I tried to extract blob and saved it as a gif file same as your way to see if the jsp page show image or not. Strangly, the saved gif file even could not be opened by photoshop(jsp too). So I suspect the reason why jsp can not show blob image from database is that the data format has been changed somehow during inserting and retriving to/from database. I don't know why it could happen. By the way, I only set one response header:response.setContentType = "image/gif".

Do you have any ideas? Thanks a lot!

Honour
20 years ago
JSP
Sorry, a little typing mistake from my provious message. The contentID in my jsp page shouldn't be important as I already hardcoded it in the servlet class.
20 years ago
JSP
Hello everyone,

I have searched all the information about how to display images from database using jsp in this forum, but none of the problems is samilar to mine.

I used a servlet class to retrieve the image blob data and wrote it to the response outputstream.

-------------------------------------------------------------------
String getResourceSql = "select content from tblContent where contentID = 9";
ResultSet docRs = stat.executeQuery(getResourceSql);

if (docRs.next()){
byte[] doc = (byte[])(docRs.getObject("content"));
System.out.println("data length " + doc.length);
ServletOutputStream ostr = response.getOutputStream();
ostr.write(doc);
ostr.flush();
ostr.close();
System.out.println("test");
}

---------------------------------------------------------------------

And after, I wrote a jsp file to call the servelt to display the image as below:

<html>
<body>

<img src="/meg/ImageServlet?contentId=8"/>

</body>
</html>

I could get the servlet test message (data length and "test" string ) printed out in Tomcat dos window, which means that the servlet was called successfully. However, the image could not be displayed in the jsp page (only a red cross). I wonder what is wrong with my code?

Thanks a lot in advance!
20 years ago
JSP