/** Open a download dialog to save the specified content at the client * with the suggested file name.<br/> * Note: You don't need to close the content (a Reader), it will be closed automatically after download. * @param content the content * @param contentType the content type (a.k.a., MIME type), * e.g., application/pdf * @param flnm the suggested file name, e.g., myfile.pdf. * If null, no suggested name is provided. */ public static void save(Reader content, String contentType, String flnm) { save(new AMedia(flnm, null, contentType, content), flnm); }
public String getStringData() { if (_strdata != null) return _strdata; Reader reader = _rddata == DYNAMIC_READER ? getReaderData() : _rddata; //ZK-938 if (reader != null) { try { String ct = Files.readAll(reader).toString(); reader.close(); return ct; } catch (java.io.IOException ex) { throw SystemException.Aide.wrap(ex); } } throw newIllegalStateException(); } /** Returns the input stream of this media.
public byte[] getByteData() { if (_bindata != null) return _bindata; InputStream is = _isdata == DYNAMIC_STREAM ? getStreamData() : _isdata ; //ZK-938 if (is != null) { try { byte[] bs = Files.readAll(is); is.close(); return bs; } catch (java.io.IOException ex) { throw SystemException.Aide.wrap(ex); } } throw newIllegalStateException(); } public String getStringData() {
/** Construct with name, format, content type and binary data. * * <p>It tries to construct format and ctype from each other or name. * * @param name the name (usually filename); might be null. * @param format the format; might be null. Example: "html" and "xml" * @param ctype the content type; might be null. Example: "text/html" * and "text/xml;charset=UTF-8". * @param data the binary data; never null */ public AMedia(String name, String format, String ctype, byte[] data) { if (data == null) throw new IllegalArgumentException("data"); _bindata = data; setup(name, format, ctype); } /** Construct with name, format, content type and text data.
/** Returns the reader of this media to retrieve the data. * * <p>Note: the caller has to invoke {@link Reader#close} * after using the input stream returned by {@link #getReaderData}. * * @exception IllegalStateException if the media is binary * {@link #isBinary}. */ public Reader getReaderData() { if (_rddata != null) return _rddata; if (_strdata != null) return new StringReader(_strdata); throw newIllegalStateException(); } private IllegalStateException newIllegalStateException() {
/** Construct with name, format, content type and text data. * * <p>It tries to construct format and ctype from each other or name. * * @param name the name (usually filename); might be null. * @param format the format; might be null. * @param ctype the content type; might be null. * @param data the text data; never null */ public AMedia(String name, String format, String ctype, String data) { if (data == null) throw new IllegalArgumentException("data"); _strdata = data; setup(name, format, ctype); } /** Construct with name, format, content type and stream data (binary).
/** Returns the input stream of this media. * * <p>Note: the caller has to invoke {@link InputStream#close} * after using the input stream returned by {@link #getStreamData}. * * @exception IllegalStateException if the media is not binary * {@link #isBinary}. */ public InputStream getStreamData() { if (_isdata != null) return _isdata; if (_bindata != null) return new ByteArrayInputStream(_bindata); throw newIllegalStateException(); } /** Returns the reader of this media to retrieve the data.
/** Open a download dialog to save the specified content at the client * with the suggested file name.<br/> * Note: You don't need to close the content (a InputStream), it will be closed automatically after download. * @param content the content * @param contentType the content type (a.k.a., MIME type), * e.g., application/pdf * @param flnm the suggested file name, e.g., myfile.pdf. * If null, no suggested name is provided. */ public static void save(InputStream content, String contentType, String flnm) { save(new AMedia(flnm, null, contentType, content), flnm); }
/** Construct with name, format, content type and reader data (textual). * * <p>It tries to construct format and ctype from each other or name. * * @param name the name (usually filename); might be null. * @param format the format; might be null. * @param ctype the content type; might be null. * @param data the string data; never null * If the reader is created dynamically each time {@link #getReaderData} * is called, you shall pass {@link #DYNAMIC_READER} * as the data argument. Then, override {@link #getReaderData} to return * the correct reader. */ public AMedia(String name, String format, String ctype, Reader data) { if (data == null) throw new IllegalArgumentException("data"); _rddata = data; setup(name, format, ctype); } /** Construct with name, format, content type and a file.
/** Open a download dialog to save the specified content at the client * with the suggested file name. * * @param content the content * @param contentType the content type (a.k.a., MIME type), * e.g., application/pdf * @param flnm the suggested file name, e.g., myfile.pdf. * If null, no suggested name is provided. */ public static void save(byte[] content, String contentType, String flnm) { save(new AMedia(flnm, null, contentType, content), flnm); }
/** Construct with name, format, content type and stream data (binary). * * <p>It tries to construct format and ctype from each other or name. * * @param name the name (usually filename); might be null. * @param format the format; might be null. * @param ctype the content type; might be null. * @param data the binary data; never null. * If the input stream is created dynamically each time {@link #getStreamData} * is called, you shall pass {@link #DYNAMIC_STREAM} * as the data argument. Then, override {@link #getStreamData} to return * the correct stream. * Note: the caller of {@link #getStreamData} has to close * the returned input stream. */ public AMedia(String name, String format, String ctype, InputStream data) { if (data == null) throw new IllegalArgumentException("data"); _isdata = data; setup(name, format, ctype); } /** Construct with name, format, content type and reader data (textual).
/** Open a download dialog to save the specified content at the client * with the suggested file name. * * @param content the content * @param contentType the content type (a.k.a., MIME type), * e.g., application/pdf * @param flnm the suggested file name, e.g., myfile.pdf. * If null, no suggested name is provided. */ public static void save(String content, String contentType, String flnm) { save(new AMedia(flnm, null, contentType, content), flnm); }
name = name.substring(j + 1); setup(name, format, ctype);
/** Open a download dialog to save the specified file at the client. * * @param file the file to download to the client * @param contentType the content type, e.g., application/pdf. * Unlike other save methods, it is optional. If null, the file name's * extension is used to determine the content type. * @exception FileNotFoundException if the file is not found. * @since 3.0.8 */ public static void save(File file, String contentType) throws FileNotFoundException { save(new AMedia(file, contentType, null), file.getName()); }
/** Construct with name, format, content type and a file. * * <p>Unlike others, it uses the so-called repeatable input * stream or reader (depending on charset is null or not) * to represent the file, so the input stream ({@link #getStreamData}) * or the reader ({@link #getReaderData}) will be re-opened * in the next invocation of {@link InputStream#read} * after {@link InputStream#close} is called. * See also {@link RepeatableInputStream} and {@link RepeatableReader}. * * @param name the name (usually filename); might be null. * If null, the file name is used. * @param format the format; might be null. * @param ctype the content type; might be null. * @param file the file; never null. * @param charset the charset. If null, it is assumed to be binary. */ public AMedia(String name, String format, String ctype, File file, String charset) throws java.io.FileNotFoundException { if (file == null) throw new IllegalArgumentException("file"); if (charset == null) _isdata = RepeatableInputStream.getInstance(file); else _rddata = RepeatableReader.getInstance(file, charset); if (name == null) name = file.getName(); setup(name, format, ctype); } /** Construct with a file.
/** * When output file type is HTML, return image in AMedia * * @param imageName the request image file name * @return A AMdia contains a image. */ private AMedia getImage(String imageName) { if (_imageMap == null) { log.warning("The image map not ready, "+imageName); return null; } byte[] imageBytes = (byte[])_imageMap.get(imageName); return new AMedia(imageName, "", "image/gif", imageBytes); } }
/** Open a download dialog to save the resource of the specified URL * at the client. * The path must be retrievable by use of {@link org.zkoss.zk.ui.WebApp#getResource}. * * @param url the URL to get the resource * @param contentType the content type, e.g., application/pdf. * Unlike other save methods, it is optional. If null, the path's * extension is used to determine the content type. * @exception FileNotFoundException if the resource is not found. * @since 3.0.8 */ public static void save(URL url, String contentType) throws FileNotFoundException { String name = url.toExternalForm(); int j = name.lastIndexOf('/'); if (j >= 0 && j < name.length() - 1) name = name.substring(j + 1); save(new AMedia(url, contentType, null), name); }
@Command public void exportListboxToExcel(@BindingParam("ref") Listbox listbox) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ExcelExporter exporter = new ExcelExporter(); exporter.export(listbox, out); AMedia amedia = new AMedia("FirstReport.xlsx", "xls", "application/file", out.toByteArray()); Filedownload.save(amedia); out.close(); }
Workbook workbook = new HSSFWorkbook(); Sheet listSheet = workbook.createSheet("KiÅŸi Listesi"); int rowIndex = 0; for (KayitParam kp : kayitList) { Row row = listSheet.createRow(rowIndex++); int cellIndex = 0; row.createCell(cellIndex++).setCellValue(kp.getAd()); row.createCell(cellIndex++).setCellValue(kp.getSoyad()); row.createCell(cellIndex++).setCellValue(kp.getEposta()); row.createCell(cellIndex++).setCellValue(kp.getCinsiyet()); row.createCell(cellIndex++).setCellValue(kp.getDogumtarihi()); row.createCell(cellIndex++).setCellValue(kp.getTahsil()); } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); AMedia amedia = new AMedia("Kisiler.xls", "xls", "application/file", baos.toByteArray()); Filedownload.save(amedia); baos.close(); } catch (Exception e) { e.printStackTrace(); }
URL url = ctx.getResource(path); if (url != null) media = new AMedia(url, contentType, null); } else if (type == 'f') { File file = new File(path); source = file; media = new AMedia(file, contentType, null); } else if (type == 'u') { URL url = new URL(path); source = url; media = new AMedia(url, contentType, null);