Jsp File Browser

Task list for Project:

Please ensure that your browser has cookies enabled if you want this software to work properly...

Register as a new user

Task #43 — Download Resume Functionality Added Here

Attached to Project — Jsp File Browser

Opened by islam sayed (semsem) - 2 Oct 2008

Task Type Feature Request Severity Medium
Category File handling Reported Version 1.2
Status Unconfirmed Due in Version Undecided
Assigned To No-one Percent Complete 0% complete
Operating System All
Details hi,
I just added the ability to resume downloads , partial content to the "download file" code .
Here it is . Can you please add my name , email there :)
This code might not be the best , but I don't know how to do it otherwise ...
thanks


// Download file
else if (request.getParameter("downfile") != null) {
String filePath = request.getParameter("downfile");
File f = new File(filePath);
if (!isAllowed(f, false)){
request.setAttribute("dir", f.getParent());
request.setAttribute("error", "You are not allowed to access " + f.getAbsoluteFile());
}
else if (f.exists() && f.canRead()) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="" + f.getName()
+ """);
response.setHeader("Accept-Ranges","bytes");
String range= request.getHeader("range");
String fseek= null;
long fseekl=0 ;
if (range != null) {
fseek = range.substring((range.indexOf("=") + 1 ),( range.indexOf("-") )) ;
fseekl = Long.valueOf(fseek);
}
if (fseek != null) {
response.setHeader("Content-Range", "bytes " + fseek + "-" + (f.length()-1) + "/" + f.length() );
int conlen = (int)f.length() - (int)fseekl;
response.setContentLength(conlen);
RandomAccessFile rand = new RandomAccessFile(f,"r");
byte buffer[] = new byte[8 * 1024];
out.clearBuffer();
OutputStream out_s = new Writer2Stream(out);
rand.seek(fseekl);
copyStreamsWithoutCloseRand(rand, out_s, buffer);
rand.close();
out_s.flush();
nohtml = true;
dir_view = false;
}
else
{
response.setContentLength((int) f.length());
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
byte buffer[] = new byte[8 * 1024];
out.clearBuffer();
OutputStream out_s = new Writer2Stream(out);
copyStreamsWithoutClose(fileInput, out_s, buffer);
fileInput.close();
out_s.flush();
nohtml = true;
dir_view = false;
}
}
else {
request.setAttribute("dir", f.getParent());
request.setAttribute("error", "File " + f.getAbsolutePath()
+ " does not exist or is not readable on the server");
}
static void copyStreamsWithoutCloseRand(RandomAccessFile rand, OutputStream out, byte[] buffer)
throws IOException {
int b;
while ((b = rand.read(buffer)) != -1)
out.write(buffer, 0, b);
}

Comments (0) | Attachments (0) | Related Tasks (0/0) | Notifications (1)