Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- try {
- ZipOutputStream _zos = new ZipOutputStream( response.getOutputStream());
- ZipEntry _ze = null;
- long startTime = System.currentTimeMillis();
- long _lByteCount = 0;
- response.setContentType("application/zip");
- while (_lByteCount < 2000) {
- _ze = new ZipEntry("foo");
- _zos.putNextEntry( _ze );
- //writes 100 bytes and then waits 10 seconds
- _lByteCount += StreamWriter.write(
- new ByteArrayInputStream(DataGenerator.getOutput().toByteArray()),
- _zos );
- System.out.println("Zip: " + _lByteCount + " Time: " + ((System.currentTimeMillis() - startTime) / 1000));
- //trying to flush
- _zos.finish();
- _zos.flush();
- response.flushBuffer();
- response.getOutputStream().flush();
- }
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- flush public void flush()
- throws IOExceptionFlushes
- this output stream and forces any
- buffered output bytes to be written out. The general contract of flush
- is that calling it is an indication that, if any bytes previously
- written have been buffered by the implementation of the output stream,
- such bytes should immediately be written to their intended
- destination. If the intended destination of this stream is an
- abstraction provided by the underlying operating system, for example a
- file, then flushing the stream guarantees only that bytes previously
- written to the stream are passed to the operating system for writing;
- it does not guarantee that they are actually written to a physical
- device such as a disk drive.
- **The flush method of OutputStream does nothing.**
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.servlet.ServletContextHandler;
- import org.eclipse.jetty.servlet.ServletHolder;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
- import java.io.IOException;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- public class ZippingAndStreamingServlet {
- public static void main(String[] args) throws Exception {
- Server server = new Server(8080);
- ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
- context.setContextPath("/");
- server.setHandler(context);
- context.addServlet(new ServletHolder(new BufferingServlet()), "/*");
- server.start();
- System.out.println("Listening on 8080");
- server.join();
- }
- static class BufferingServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- ZipOutputStream _zos = new ZipOutputStream(response.getOutputStream());
- ZipEntry _ze;
- long startTime = System.currentTimeMillis();
- long _lByteCount = 0;
- int count = 1;
- response.setContentType("application/zip");
- response.setHeader("Content-Disposition", "attachment; filename=my.zip");
- while (_lByteCount < 2000) {
- _ze = new ZipEntry("foo" + count);
- _zos.putNextEntry(_ze);
- byte[] bytes = String.format("%100d", count++).getBytes();
- System.out.println("Sending " + bytes.length + " bytes");
- _zos.write(bytes);
- _lByteCount += bytes.length;
- sleep(1000);
- System.out.println("Zip: " + _lByteCount + " Time: " + ((System.currentTimeMillis() - startTime) / 1000));
- _zos.flush();
- }
- _zos.close();
- }
- private void sleep(int millis) {
- try {
- Thread.sleep(millis);
- } catch (InterruptedException e) {
- throw new IllegalStateException("Unexpected interrupt!", e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement