Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. protected void doGet(HttpServletRequest request,
  2. HttpServletResponse response) throws ServletException, IOException {
  3. try {
  4. ZipOutputStream _zos = new ZipOutputStream( response.getOutputStream());
  5. ZipEntry _ze = null;
  6. long startTime = System.currentTimeMillis();
  7. long _lByteCount = 0;
  8.  
  9. response.setContentType("application/zip");
  10.  
  11. while (_lByteCount < 2000) {
  12. _ze = new ZipEntry("foo");
  13. _zos.putNextEntry( _ze );
  14.  
  15. //writes 100 bytes and then waits 10 seconds
  16. _lByteCount += StreamWriter.write(
  17. new ByteArrayInputStream(DataGenerator.getOutput().toByteArray()),
  18. _zos );
  19. System.out.println("Zip: " + _lByteCount + " Time: " + ((System.currentTimeMillis() - startTime) / 1000));
  20.  
  21. //trying to flush
  22. _zos.finish();
  23. _zos.flush();
  24. response.flushBuffer();
  25. response.getOutputStream().flush();
  26. }
  27. } catch (Throwable e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. flush public void flush()
  33. throws IOExceptionFlushes
  34. this output stream and forces any
  35. buffered output bytes to be written out. The general contract of flush
  36. is that calling it is an indication that, if any bytes previously
  37. written have been buffered by the implementation of the output stream,
  38. such bytes should immediately be written to their intended
  39. destination. If the intended destination of this stream is an
  40. abstraction provided by the underlying operating system, for example a
  41. file, then flushing the stream guarantees only that bytes previously
  42. written to the stream are passed to the operating system for writing;
  43. it does not guarantee that they are actually written to a physical
  44. device such as a disk drive.
  45.  
  46. **The flush method of OutputStream does nothing.**
  47.  
  48. import org.eclipse.jetty.server.Server;
  49. import org.eclipse.jetty.servlet.ServletContextHandler;
  50. import org.eclipse.jetty.servlet.ServletHolder;
  51.  
  52. import javax.servlet.ServletException;
  53. import javax.servlet.http.*;
  54. import java.io.IOException;
  55. import java.util.zip.ZipEntry;
  56. import java.util.zip.ZipOutputStream;
  57.  
  58. public class ZippingAndStreamingServlet {
  59. public static void main(String[] args) throws Exception {
  60. Server server = new Server(8080);
  61. ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  62. context.setContextPath("/");
  63. server.setHandler(context);
  64. context.addServlet(new ServletHolder(new BufferingServlet()), "/*");
  65. server.start();
  66. System.out.println("Listening on 8080");
  67. server.join();
  68. }
  69.  
  70. static class BufferingServlet extends HttpServlet {
  71. protected void doGet(HttpServletRequest request,
  72. HttpServletResponse response) throws ServletException, IOException {
  73. ZipOutputStream _zos = new ZipOutputStream(response.getOutputStream());
  74. ZipEntry _ze;
  75. long startTime = System.currentTimeMillis();
  76. long _lByteCount = 0;
  77. int count = 1;
  78. response.setContentType("application/zip");
  79. response.setHeader("Content-Disposition", "attachment; filename=my.zip");
  80. while (_lByteCount < 2000) {
  81. _ze = new ZipEntry("foo" + count);
  82. _zos.putNextEntry(_ze);
  83. byte[] bytes = String.format("%100d", count++).getBytes();
  84. System.out.println("Sending " + bytes.length + " bytes");
  85. _zos.write(bytes);
  86. _lByteCount += bytes.length;
  87. sleep(1000);
  88. System.out.println("Zip: " + _lByteCount + " Time: " + ((System.currentTimeMillis() - startTime) / 1000));
  89. _zos.flush();
  90. }
  91. _zos.close();
  92. }
  93.  
  94. private void sleep(int millis) {
  95. try {
  96. Thread.sleep(millis);
  97. } catch (InterruptedException e) {
  98. throw new IllegalStateException("Unexpected interrupt!", e);
  99. }
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement