Subject | Re: [Firebird-Java] Picture Servlet Problems possibly OT |
---|---|
Author | Rick Fincher |
Post date | 2003-06-06T01:20:35Z |
Hi William,
You responded to my question about the performance of blob reads a few weeks
ago with a code snippet using getByte.
I had similar problems to yours because I was using Win 2000 to develop and
Linux to deploy.
I also had trouble with certain file types, pdf in particular.
I figured it was text translation related so I used the code below, similar
to what is in the FAQ.
This worked for Win 2000 and Linux for jpeg, Acrobat, gif, Microsoft Word,
and PowerPoint files. Essentially any file the browser knew the MIME type
of or had a plugin for.
Some of my files were many megabytes so this only uses a small memory
buffer. If your files are small and you can afford the memory make your
buffer bigger than your average file and you will get better performance.
Keep in mind that you have a separate buffer for each simultaneous download,
so a busy site might chew up a lot of RAM.
That's in the "new byte[2048]" line.
Hope this helps,
Rick
------------------------------------------
// BINARYFILE is the column name of the blob
// MIMETYPE is the column name of the MIME type of the file stored when the
file is uploaded as a blob.
response.setContentType(rs.getString("MIMETYPE"));
// Create an input stream from the BLOB column. By default,
rs.getBinaryStream()
// returns a vanilla InputStream instance. We override this for efficiency
// but you don't have to:
BufferedInputStream bis = new BufferedInputStream(
rs.getBinaryStream("BINARYFILE"));
javax.servlet.ServletOutputStream out = response.getOutputStream();
// A temporary buffer for the byte data:
byte bindata[] = new byte[2048];
// Used to return how many bytes are read with each read() of the input
stream:
int bytesread = 0;
while ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 ) {
// Write out 'bytesread' bytes to the writer instance:
out.write(bindata,0,bytesread);
}
// Close the binary input stream:
bis.close();
out.flush();
out.close();
You responded to my question about the performance of blob reads a few weeks
ago with a code snippet using getByte.
I had similar problems to yours because I was using Win 2000 to develop and
Linux to deploy.
I also had trouble with certain file types, pdf in particular.
I figured it was text translation related so I used the code below, similar
to what is in the FAQ.
This worked for Win 2000 and Linux for jpeg, Acrobat, gif, Microsoft Word,
and PowerPoint files. Essentially any file the browser knew the MIME type
of or had a plugin for.
Some of my files were many megabytes so this only uses a small memory
buffer. If your files are small and you can afford the memory make your
buffer bigger than your average file and you will get better performance.
Keep in mind that you have a separate buffer for each simultaneous download,
so a busy site might chew up a lot of RAM.
That's in the "new byte[2048]" line.
Hope this helps,
Rick
------------------------------------------
// BINARYFILE is the column name of the blob
// MIMETYPE is the column name of the MIME type of the file stored when the
file is uploaded as a blob.
response.setContentType(rs.getString("MIMETYPE"));
// Create an input stream from the BLOB column. By default,
rs.getBinaryStream()
// returns a vanilla InputStream instance. We override this for efficiency
// but you don't have to:
BufferedInputStream bis = new BufferedInputStream(
rs.getBinaryStream("BINARYFILE"));
javax.servlet.ServletOutputStream out = response.getOutputStream();
// A temporary buffer for the byte data:
byte bindata[] = new byte[2048];
// Used to return how many bytes are read with each read() of the input
stream:
int bytesread = 0;
while ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 ) {
// Write out 'bytesread' bytes to the writer instance:
out.write(bindata,0,bytesread);
}
// Close the binary input stream:
bis.close();
out.flush();
out.close();
----- Original Message -----
> I am trying to track down a problem I am having with a servlet that
> servers pictures from Blob columns in a Firebird DB.
>
> The same servlet does not generate the same results on different
> platforms.
>
> The db server is the same for all machines. My DB server currently is a
> RH 8 box, with a 2.4 kernel, and UTF8 as it's default character set.
>
> My DB's default character set is ASCII. I choose that a long time ago
> and plan to pump out the data into a UTF8 db. Hopefully that day is not
> today.
>
> The servlet only produces the expected output on one of my machines.
> That machines is running a 2.2 kernel, and the OS is based on RH 6.2.
> It's default character set is ISO8859-1.
>
> Now I have two other machines that I have the same servlet talking to
> the same db server, yet the output of the servlet differs.
>
> One of the machines is a RH 9 box, with a 2.4 kernel and it's default
> character set is UTF8.
>
> Another machine is a Win2k machine. Not sure on default character set,
> but I think it is UTF8?
>
> Now one all three machines I am running the latest version of Tomcat,
> JDK from Sun, and JayBird.
>
> I have spent some time on this and for the life of me can't find the
> problem. I have played around with the servlet itself, but I do not
> think that is the problem.
>
> I think the problem is created during the conversion of character sets.
> I have tried to play around with the lc_ctype, but it seems to have no
> effect.
>
> I do not believe this is a Tomcat issue, maybe JDK? I do not think it's
> a JayBird or Firebird problem. I do think I am not doing something
> correctly with regard to Firebird, Jaybird and character sets.
>
> Here is the main part of the servlet
>
> if(result_set.next()) {
> //ServletOutputStream sos = response.getOutputStream();
> //sos.println(result_set.getString(1));
> //sos.close();
> PrintWriter print_writer = response.getWriter();
> response.setContentType("image/jpeg");
> response.setContentLength(result_set.getString(1).length());
> print_writer.print(result_set.getString(1));
> print_writer.close();
> }
>
> Now when I use ServletOutputStream, it sort of confirms my character set
> theory. Since I get errors like
>
> java.io.CharConversionException: Not an ISO 8859-1 character: �
>
> Now if I change the character set of the browser to UTF8 I get the
> following
>
> java.io.CharConversionException: Not an ISO 8859-1 character: �
>
> That last char shows up as a box on Win2k.
>
> Now on both my Linux machines I am also using Apache. However on the
> Windows machines I am only using Tomcat.
>
> So my problem appears to be with character set conversions. I assume
> between the output from Jaybird and the viewers browser? Possible having
> to do with Tomcat?
>
> Here are links to the output of the servlet on different machines when
> using PrintWriter.
>
> Here is a picture from the servlet on my production server
> http://www.obsidian-studios.com/Picture1.jpg
>
> Here is that same picture from my development machine and win2k machine.
> http://www.obsidian-studios.com/Picture.jpg
>
> It appears the pictures are off by one byte. However if you look at the
> two files with a hex editor, their contents are quite different. More
> than just a lack of a byte. Thus the result being a crappy picture.
>
> Any suggestions on how I might solve this problem?
>
> I am sort of stuck, and really need assistance. Thank you.
>
> --
> Sincerely,
> William L. Thomson Jr.
> Support Group
> Obsidian-Studios, Inc.
> 3548 Jamestown Ln.
> Jacksonville, FL 32223
> Phone/Fax 904.260.2445
> http://www.obsidian-studios.com
>
>
>
> To unsubscribe from this group, send an email to:
> Firebird-Java-unsubscribe@yahoogroups.com
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>