您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FileDownloaderUI.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2012 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components;
  17. import java.awt.image.BufferedImage;
  18. import java.io.BufferedOutputStream;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import javax.imageio.ImageIO;
  28. import com.vaadin.server.ClassResource;
  29. import com.vaadin.server.ConnectorResource;
  30. import com.vaadin.server.FileDownloader;
  31. import com.vaadin.server.FileResource;
  32. import com.vaadin.server.StreamResource;
  33. import com.vaadin.server.VaadinRequest;
  34. import com.vaadin.server.VaadinResponse;
  35. import com.vaadin.tests.components.embedded.EmbeddedPdf;
  36. import com.vaadin.ui.AbstractComponent;
  37. import com.vaadin.ui.Button;
  38. import com.vaadin.ui.Component;
  39. import com.vaadin.ui.CssLayout;
  40. import com.vaadin.ui.HorizontalLayout;
  41. import com.vaadin.ui.Label;
  42. import com.vaadin.ui.Layout;
  43. import com.vaadin.ui.NativeButton;
  44. public class FileDownloaderUI extends AbstractTestUIWithLog {
  45. private AbstractComponent firstDownloadComponent;
  46. @Override
  47. protected void setup(VaadinRequest request) {
  48. List<Class<? extends Component>> components = new ArrayList<>();
  49. components.add(Button.class);
  50. components.add(NativeButton.class);
  51. components.add(CssLayout.class);
  52. components.add(Label.class);
  53. ConnectorResource resource;
  54. resource = new StreamResource(new StreamResource.StreamSource() {
  55. @Override
  56. public InputStream getStream() {
  57. try {
  58. BufferedImage img = getImage2("demo.png");
  59. ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
  60. ImageIO.write(img, "png", imagebuffer);
  61. Thread.sleep(5000);
  62. return new ByteArrayInputStream(imagebuffer.toByteArray());
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. return null;
  66. }
  67. }
  68. }, "demo.png");
  69. addComponents("Dynamic image", resource, components);
  70. try {
  71. File hugeFile = File.createTempFile("huge", ".txt");
  72. hugeFile.deleteOnExit();
  73. BufferedOutputStream os = new BufferedOutputStream(
  74. new FileOutputStream(hugeFile));
  75. int writeAtOnce = 1024 * 1024;
  76. byte[] b = new byte[writeAtOnce];
  77. for (int i = 0; i < 5l * 1024l * 1024l; i += writeAtOnce) {
  78. os.write(b);
  79. }
  80. os.close();
  81. resource = new FileResource(hugeFile);
  82. addComponents("Huge text file", resource, components);
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. resource = new ClassResource(new EmbeddedPdf().getClass(), "test.pdf");
  87. addComponents("Class resource pdf", resource, components);
  88. Button downloadUtf8File = new Button("Download UTF-8 named file");
  89. FileDownloader fd = new FileDownloader(new ClassResource(
  90. new EmbeddedPdf().getClass(), "File åäö-日本語.pdf"));
  91. fd.setOverrideContentType(false);
  92. fd.extend(downloadUtf8File);
  93. addComponent(downloadUtf8File);
  94. addComponent(
  95. new Button("Remove first download button", event -> {
  96. Layout parent = (Layout) firstDownloadComponent.getParent();
  97. parent.removeComponent(firstDownloadComponent);
  98. }));
  99. addComponent(
  100. new Button("Detach FileDownloader from first download button",
  101. event -> {
  102. FileDownloader e = (FileDownloader) firstDownloadComponent
  103. .getExtensions().iterator().next();
  104. e.remove();
  105. log("FileDownload detached");
  106. }));
  107. }
  108. public void addComponents(String caption, ConnectorResource resource,
  109. List<Class<? extends Component>> components) {
  110. HorizontalLayout layout = new HorizontalLayout();
  111. layout.setCaption(caption);
  112. for (Class<? extends Component> cls : components) {
  113. try {
  114. AbstractComponent c = (AbstractComponent) cls.newInstance();
  115. if (firstDownloadComponent == null) {
  116. firstDownloadComponent = c;
  117. }
  118. c.setId(cls.getName() + caption.replace(" ", ""));
  119. c.setCaption(cls.getName());
  120. c.setDescription(
  121. resource.getMIMEType() + " / " + resource.getClass());
  122. c.setWidth("100px");
  123. c.setHeight("100px");
  124. layout.addComponent(c);
  125. new FileDownloader(resource).extend(c);
  126. if (c instanceof Button) {
  127. ((Button) c).addClickListener(event -> {
  128. });
  129. }
  130. } catch (Exception e) {
  131. System.err.println("Could not instatiate " + cls.getName());
  132. }
  133. }
  134. addComponent(layout);
  135. }
  136. private static final String DYNAMIC_IMAGE_NAME = "requestImage.png";
  137. @Override
  138. public boolean handleConnectorRequest(VaadinRequest request,
  139. VaadinResponse response, String path) throws IOException {
  140. if (DYNAMIC_IMAGE_NAME.equals(path)) {
  141. // Create an image, draw the "text" parameter to it and output it to
  142. // the browser.
  143. String text = request.getParameter("text");
  144. if (text == null) {
  145. text = DYNAMIC_IMAGE_NAME;
  146. }
  147. BufferedImage bi = getImage(text);
  148. response.setContentType("image/png");
  149. response.setHeader("Content-Disposition",
  150. "attachment; filename=\"" + path + "\"");
  151. ImageIO.write(bi, "png", response.getOutputStream());
  152. return true;
  153. } else {
  154. return super.handleConnectorRequest(request, response, path);
  155. }
  156. }
  157. private BufferedImage getImage(String text) {
  158. BufferedImage bi = new BufferedImage(150, 30,
  159. BufferedImage.TYPE_3BYTE_BGR);
  160. bi.getGraphics().drawChars(text.toCharArray(), 0, text.length(), 10,
  161. 20);
  162. return bi;
  163. }
  164. private BufferedImage getImage2(String text) {
  165. BufferedImage bi = new BufferedImage(200, 200,
  166. BufferedImage.TYPE_INT_RGB);
  167. bi.getGraphics().drawChars(text.toCharArray(), 0, text.length(), 10,
  168. 20);
  169. return bi;
  170. }
  171. }