Re: [Simpleweb-Support] File upload progress
Brought to you by:
niallg
|
From: -=}\\*/{=- <rui...@gm...> - 2012-07-17 13:27:51
|
the server that i'm implementing over simple implements uploads...
since i inherit the code from previous version i did not check if simple
supports this... any way i leave you the code...
may not be perfect but is what i got... maybe you find it useful.
/*
* Copyright 2005 Rui Damas
*/
package abc.kis.handler;
import abc.kis.Quest;
import abc.kis.Sponse;
import abc.kis.handler.abs.FileNameMatcher;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.Date;
/**
* A kiss handler to alow file download (upload to the client) width
* support for the client to interrupt the download and continue later.
*/
public class Downloader extends FileNameMatcher {
/**
* Uploads a requested file to the client if it exists...
* requires mime type mapping.
*
* <p>
* Tries to resolve a file (using the <code>toHandle</code> resolver).
* if such file is found, it will be sent to the client.<br/>
* Before sending the content type is set using the KISSLetConfig mime
mapper
* and the <code>"Date"</code> header is set to the file's last modified
* date.<br/>
* Simple skipping (continue download) is supported if an header named
* 'range' is found in the request and it's value is 'bytes=????-'
* where ???? is the number of bytes to skip in the file's stream to
upload
* to the client.
* </p>
*
* @return always <code>true</code>.
*/
@Override
public boolean handle(Quest quest, Sponse sponse,
URLConnection toUpload) {
try {
// Support (simple) skipping
long firstByteIndex = 0;
String range = quest.getHeader("Range");
if (range != null) {
if (range.startsWith("bytes=")) {
range = range.substring("bytes=".length());
int minusIndex = range.indexOf('-');
if (minusIndex > 0) {
range = range.substring(0, minusIndex);
}
try {
firstByteIndex = Long.parseLong(range);
quest.logI("Skipping: %d bytes", firstByteIndex);
} catch (NumberFormatException nfe) {
quest.logT(nfe, "Unable to parse requested range");
}
}
}
// get stream and skip to first byte position
InputStream toSend = toUpload.getInputStream();
toSend.skip(firstByteIndex);
// set response headers
long toSendLength = toUpload.getContentLength();
sponse.setContentLength((int) (toSendLength - firstByteIndex));
sponse.setHeader("Content-range", "bytes " + firstByteIndex + "-"
+ (toSendLength - 1) + "/" + toSendLength);
// set content type
String mimeType = quest.getMimeTypes().
getMimeTypeOrDefaultForPath(toUpload.getURL().getPath());
sponse.setContentType(mimeType);
// set date to file's last modified date
long lastModified = toUpload.getLastModified();
long current = new Date().getTime();
long deltaSeconds = (current - lastModified) / 1000;
int deltaSinceCurrent = (60 * 5);
sponse.setHeader("Cache-Control", "s-maxage="
+ deltaSeconds + deltaSinceCurrent);
sponse.setDate("Date", current);
sponse.setDate("Expires", current + (1000 * deltaSinceCurrent));
sponse.setDate("Last-Modified", lastModified);
// send file stream
sponse.send(toSend);
quest.logI("Sent as \"%1$s\" ", mimeType);
} catch (IOException ioe) {
quest.logT(ioe, "File not completely sent");
}
return true;
}
}
On 17 July 2012 12:47, Andrew Barlow <and...@de...> wrote:
> I wondered if the built in file upload mechanism for Simple would work
> with client-side upload "widgets" to show progress bars, etc.
>
> For example http://blueimp.github.com/jQuery-File-Upload/.
>
> Also, has anyone looked at being able to resume stalled or partial
> uploads, and how this might be achieved with Simple?
> Regards,
> Andy Barlow
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Simpleweb-Support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simpleweb-support
>
|