sending data to browser via servlet: windows vs. unix


L

Lord Zoltar

I wrote a small servlet for a web application to send pdf files
(and other types of files) from a local filesystem to a user's web
browser. When I run the application locally (my workstation runs
Windows), everything works fine. When I deploy to the testing server
(which runs UNIX), the data does not get transmitted properly. Trying
to get a PDF file causes Acrobat to return an error of "The root
object is missing or invalid". Trying to retrieve a Word file via the
servlet causes Word to try to convert from some unidentified encoding
(which fails and results in garbage on the screen).

The problem seems to be that the same code is not sending the data
correctly when run on the UNIX server. I'm not sure why this would be,
I had thought that Java was supposed to handle differences between
filesystems on its own. Any ideas?

simplified testing code looks like this:

public class FileServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
//The full path to the file comes in on a parameter named file.
String filePath = request.getParameter("file");
String fileName = filePath.substring(filePath.lastIndexOf('/')+1,
filePath.length());
String fileExtension = fileName.substring(fileName.lastIndexOf('.')
+1,fileName.length());

if (fileExtension.toUpperCase().equals(ContentTypes.PDF.toString
()))
{
response.setContentType(ContentTypes.PDF.getMIMEType());
}
else if (fileExtension.toUpperCase().equals
(ContentTypes.DOC.toString()))
{
response.setContentType(ContentTypes.DOC.getMIMEType());
}

//Now we'll try to open the file.
File file = new File(filePath);
fileInStream = new FileInputStream(file);
response.setContentLength((int)file.length());
stream = response.getOutputStream();
int data=fileInStream.read();
while(data!=-1)
{
stream.write(data);
data=fileInStream.read();
}
}
catch (IOException ioe)
{
throw new ServletException(ioe.getMessage());
}
finally
{
if (fileInStream!=null)
fileInStream.close();
if (stream!=null)
stream.close();
}
}
}
}
 
Ad

Advertisements

J

John B. Matthews

Lord Zoltar said:
I wrote a small servlet for a web application to send pdf files (and
other types of files) from a local filesystem to a user's web
browser. When I run the application locally (my workstation runs
Windows), everything works fine. When I deploy to the testing server
(which runs UNIX), the data does not get transmitted properly. Trying
to get a PDF file causes Acrobat to return an error of "The root
object is missing or invalid". Trying to retrieve a Word file via the
servlet causes Word to try to convert from some unidentified encoding
(which fails and results in garbage on the screen).

The problem seems to be that the same code is not sending the data
correctly when run on the UNIX server. I'm not sure why this would
be, I had thought that Java was supposed to handle differences
between filesystems on its own. Any ideas?

Do these match your servlet container's mime types?
ContentTypes.PDF.getMIMEType()
ContentTypes.DOC.getMIMEType()

Does it work if the servlet container serves the content directly?

[...]
 
L

Lord Zoltar

    I wrote a small servlet for a web application to send pdf files
(and other types of files) from a local filesystem to a user's web
browser. When I run the application locally (my workstation runs
Windows), everything works fine. When I deploy to the testing server
(which runs UNIX), the data does not get transmitted properly. Trying
to get a PDF file causes Acrobat to return an error of "The root
object is missing or invalid". Trying to retrieve a Word file via the
servlet causes Word to try to convert from some unidentified encoding
(which fails and results in garbage on the screen).

    The problem seems to be that the same code is not sending the data
correctly when run on the UNIX server. I'm not sure why this would be,
I had thought that Java was supposed to handle differences between
filesystems on its own. Any ideas?

Never mind, the files on the test system had been systematically
corrupted when they were moved over to it, all fixed now. The Java
code to send the data from the servlet does indeed work on Windows and
UNIX the same, just as I'd expected :)
 
A

Arne Vajhøj

Lord said:
Never mind, the files on the test system had been systematically
corrupted when they were moved over to it, all fixed now. The Java
code to send the data from the servlet does indeed work on Windows and
UNIX the same, just as I'd expected :)

:)

Arne
 
A

Arne Vajhøj

Lord said:
int data=fileInStream.read();
while(data!=-1)
{
stream.write(data);
data=fileInStream.read();

Your problem is already solved, but:

byte[] data = new byte[51200];
int n;
while((n = fileInStream.read(b)) >= 0) {
stream.write(b, 0, n);
}

may be a little bit more efficient.

Arne
 
L

Lord Zoltar

Lord said:
                           int data=fileInStream.read();
                           while(data!=-1)
                           {
                                   stream.write(data);
                                   data=fileInStream.read();

Your problem is already solved, but:

byte[] data = new byte[51200];
int n;
while((n = fileInStream.read(b)) >= 0) {
     stream.write(b, 0, n);

}

may be a little bit more efficient.

Arne

Hmm good point. I may give this a try. Thanks!
 
Ad

Advertisements

R

Roedy Green

rying
to get a PDF file causes Acrobat to return an error of "The root
object is missing or invalid".

A few things to check:

use http://mindprod.com/applet/mimecheck.html
to make sure your server is sending the right mimetypes.

make sure your FTP upload is treating PDF as binary.

IF you are downloading PDFs and leaving them on local client disk,
make sure they are the right length and compute a checksum. Make sure
it matches. See http://mindprod.com/jgloss/checksum.html

--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 

Top