Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
JSP: IOException
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
lasa
n00b
n00b


Joined: 17 May 2002
Posts: 63
Location: Stockholm, Sweden

PostPosted: Tue May 28, 2002 2:36 pm    Post subject: JSP: IOException Reply with quote

I am running resin 2.1.1 in standalone mode and when I recently wrote a JSP for downloading files I ran into trouble. The files that are supposed to be downloaded are iso images averaging 600MB in size. Everything works very well, and files can be completely downloaded, but the problems begin when a recipient decides to cancel a download before it is finished. Let's assume that a download is cancelled by pressing "Cancel" in the IE dialog box after only a few kilobytes have been transferred. One would assume that an IOException would be thrown next time outStream.write(buffer, 0, bytes) is called, but no, the while-loop continues to trod along like nothing happened until it reaches the end of the file. It seems like Resin is aware of these "stale" downloads, since it refuses me to download anything after two cancelled ones. I can also keep track of two open java processes that uses more CPU the higher the transfer speed limit is set. If I remove the limit a cycle in the while loop will execute very fast after the download is cancelled, bringing the process up to about 90% load. I've been trying to find information as to why no IOException is thrown when the connection with the browser is lost after the cancellation of the download, but I haven't found any. Should anyone have any ideas I would really appreciate to hear them.
Code:

<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%!
// points to where the files are kept
String baseDirectory="/home/subside/iso";
// speed limit in kB/s
int LIMIT=10;
%>
<%
  String host = request.getRemoteHost();
  String filename = null;
  try {
     filename = request.getParameter("file");
  }
  catch(Exception e) {
     response.sendError(500, "Internal Server Error: " + e);
  }
  File file = new File(baseDirectory + "/" + filename);
  System.out.println("(" + host + ") downloading: " + file.getName());
  int buffersize = response.getBufferSize();
  response.reset();
  response.setContentType("application/octet-stream");
  response.setContentLength((int)file.length());
  response.setHeader("Content-Disposition","attachment; filename=\""+ file.getName() + "\"");
  response.setHeader("Cache-Control", "no-cache");
  response.setHeader("Pragma", "no-cache");
  response.setHeader("Connection", "close");
  response.flushBuffer();
  BufferedInputStream inStream = null;
  BufferedOutputStream outStream = null;
  try {
     inStream = new BufferedInputStream(new FileInputStream(file));
     outStream = new BufferedOutputStream(response.getOutputStream());
     byte[] buffer = new byte[buffersize];
     int bytes;
     long delay = (1000*buffersize)/(LIMIT*1024);
     while((bytes = inStream.read(buffer, 0, buffersize)) != -1) {
        outStream.write(buffer, 0, bytes);
        outStream.flush();
        Thread.currentThread().sleep(delay);
     }
  }
  catch(IOException e) {
     System.out.println("(" + host + ") error: " + e);
     e.printStackTrace();
  }
  finally {
     if(inStream != null)
       inStream.close();
     if(outStream != null)
       outStream.close();
  }
  System.out.println("(" + host + ") completed: " + file.getName());
%>
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum