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();
}
}
}
}
(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();
}
}
}
}