I have a Java servlet that is used to download files. It works splendidly on Netscape.
I have a problem with some versions of IE. After I download - using the "save to disk"
option the browser hangs. Any clue?
Below is the doGet function of my code. Client2 is a class that handles the socket
functions.
public void doGet (HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{//start doGet
final int ERROR_SUCCESS = 0;
final int ERROR_RECEIVING_FROM_SOCKET = 6;
final int ERROR_FILE_REQUESTED_NOT_FOUND = 16;
final int ERROR_ACCESS_IS_DENIED = 18;
final int ERROR_COULD_NOT_SET_MODE = 19;
//PrintWriter pwOut;
DataInputStream dsInput = null;
DataOutputStream dsOutput = null;
ServletOutputStream osOutput = null;
String sGetFile = null;
String sAppID = null;
String sIPAddress = null;
String sFilePath = null;
String sContentType = "text/html";
String sExtension = null;
String sCheckForError = null;
String sBuffer = "";
String sFileName = null;
String sFileLength = null;
Client2 cClient = null;
int iReceived = -1;
int iSent = -1;
int iPort = 0;
int iOffset = 0;
int iRead = 0;
int iIndex = 0;
int iErrorCode = ERROR_SUCCESS;
byte bArray[] = null;
// set some response header fields first
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
//get the command line variables
sFilePath = FormatSpaces(request.getParameter("File"));
sAppID = request.getParameter("appid");
sIPAddress = request.getParameter("host");
iPort = Integer.parseInt(request.getParameter("port"));
iOffset = Integer.parseInt(request.getParameter("offset"));
//strip the file extension
sExtension = sFilePath.substring(sFilePath.lastIndexOf(".") + 1);
try
{//start try
cClient = new Client2(sIPAddress, iPort);
iSent = cClient.send("GET /WebFile.exe?buffer="+ sFilePath + "|" + sAppID + "|" +
iOffset);
if(iSent != -1)
{//if sent successfully, receive
bArray = cClient.receiveFirst();
}//end if sent, receive
sBuffer = new String(bArray);
if(sBuffer.length() == 0)
iErrorCode = ERROR_RECEIVING_FROM_SOCKET;
if(sBuffer.startsWith("err|"))
iErrorCode = Integer.parseInt(sBuffer.substring(4, sBuffer.indexOf((int)'|', 4)));
if(iErrorCode != ERROR_SUCCESS)
{//if error
response.setContentType("text/html");
switch (iErrorCode)
{//start switch error code
case ERROR_RECEIVING_FROM_SOCKET:
response.sendError(response.SC_SERVICE_UNAVAILABLE, "Can't receive from socket");
break;
case ERROR_FILE_REQUESTED_NOT_FOUND:
response.sendError(response.SC_NOT_FOUND, "File not found");
break;
case ERROR_ACCESS_IS_DENIED:
response.sendError(response.SC_FORBIDDEN, "Access is Denied");
break;
case ERROR_COULD_NOT_SET_MODE:
response.sendError(response.SC_BAD_GATEWAY, "Could not set mode");
break;
default:
response.sendError(response.SC_BAD_REQUEST, "Bad Request");
break;
}//end switch
}//end if error
{//start else
iIndex = sFilePath.lastIndexOf('\\');
sFileName = new String(sFilePath.substring(iIndex + 1));
//strip the file length
iIndex = sBuffer.indexOf('|');
sFileLength = new String(sBuffer.substring(4, iIndex)); //skip the word size
iIndex++;
response.setHeader("Content-Length", sFileLength);
response.setContentType(GetContentType(sExtension));
response.setHeader("Content-Disposition", "attachment; filename = \"" + sFileName +
"\"");
response.setHeader("Content-Transfer-Encoding", "8bit");
osOutput = response.getOutputStream();
osOutput.write(bArray, iIndex, bArray.length-iIndex);
osOutput.flush();
cClient.receiveRest(osOutput);
}//end else
cClient.closeClient();
}//end try
catch(ClassNotFoundException e)
{//start catch ClassNotFoundException
//pwOut.println("Class Not Found " + e.getMessage());
}//end catch ClassNotFoundException
catch(IOException e)
{//start catch IOException
//pwOut.println("IOException " + e.getMessage());
}//end catch IOException
catch(Throwable e)
{//start catch Throwable
//pwOut.println("Throwable " + e.getMessage());
}//end catch Throwable
}//end doGet
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html