/* Filedownload.java

   Purpose:

   Description:

   History:
Mon Apr 16 09:29:44 2007, Created by tomyeh Copyright (C) 2007 Potix Corporation. All Rights Reserved. {{IS_RIGHT
This program is distributed under LGPL Version 2.1 in the hope that
it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package org.zkoss.zul; import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL; import org.zkoss.util.media.AMedia;
import org.zkoss.util.media.Media;
import org.zkoss.zk.au.DeferredValue;
import org.zkoss.zk.au.out.AuDownload;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.sys.WebAppCtrl; /**
* File download utilities.
*
* @author tomyeh
* @see Fileupload
*/
public class Filedownload {
/** Open a download dialog to save the specified content at the client.
*/
public static void save(Media media) {
save(media, null);
} /** Open a download dialog to save the specified content at the client
* with the suggested file name.
*
* @param media the media to download
* @param flnm the suggested file name, e.g., myfile.pdf.
* If null, {@link Media#getName} is assumed.
*/
public static void save(Media media, String flnm) {
final Desktop desktop = Executions.getCurrent().getDesktop();
((WebAppCtrl) desktop.getWebApp()).getUiEngine().addResponse(new AuDownload(new DownloadURL(media, flnm))); //Bug 2114380
} /** 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);
} /** 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);
} /** 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);
} /** 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);
} /** 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());
} /** 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);
} /** Open a download dialog to save the resource of the specified path
* at the client.
*
* @param path the path of the resource.
* It must be retrievable by use of {@link org.zkoss.zk.ui.WebApp#getResource}.
* @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(String path, String contentType) throws FileNotFoundException {
final URL url = Executions.getCurrent().getDesktop().getWebApp().getResource(path);
if (url == null)
throw new FileNotFoundException(path);
save(url, contentType);
} private static class DownloadURL implements DeferredValue {
private final Media _media;
private final String _path; private DownloadURL(Media media, String flnm) {
_media = media; if (flnm == null)
flnm = media.getName(); final StringBuffer sb = new StringBuffer(32);
if (flnm != null && flnm.length() != 0) {
sb.append('/');
sb.append(flnm);
if (flnm.lastIndexOf('.') < 0) {
final String format = media.getFormat();
if (format != null)
sb.append('.').append(format);
}
}
_path = sb.toString();
} public Object getValue() {
return Executions.getCurrent().getDesktop().getDownloadMediaURI(_media, _path);
}
}
}

org.zkoss.zul.Filedownload.java 源码的更多相关文章

  1. 如何阅读Java源码 阅读java的真实体会

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心.   说到技术基础,我打个比 ...

  2. Android反编译(一)之反编译JAVA源码

    Android反编译(一) 之反编译JAVA源码 [目录] 1.工具 2.反编译步骤 3.实例 4.装X技巧 1.工具 1).dex反编译JAR工具  dex2jar   http://code.go ...

  3. 如何阅读Java源码

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动.源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧, ...

  4. Java 源码学习线路————_先JDK工具包集合_再core包,也就是String、StringBuffer等_Java IO类库

    http://www.iteye.com/topic/1113732 原则网址 Java源码初接触 如果你进行过一年左右的开发,喜欢用eclipse的debug功能.好了,你现在就有阅读源码的技术基础 ...

  5. Programming a Spider in Java 源码帖

    Programming a Spider in Java 源码帖 Listing 1: Finding the bad links (CheckLinks.java) import java.awt. ...

  6. 解密随机数生成器(二)——从java源码看线性同余算法

    Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...

  7. Java--Eclipse关联Java源码

    打开Eclipse,Window->Preferences->Java 点Edit按钮后弹出: 点Source Attachment后弹出: 选择Java安装路径下的src.zip文件即可 ...

  8. 使用JDT.AST解析java源码

    在做java源码的静态代码审计时,最基础的就是对java文件进行解析,从而获取到此java文件的相关信息: 在java文件中所存在的东西很多,很复杂,难以用相关的正则表达式去一一匹配.但是,eclip ...

  9. [收藏] Java源码阅读的真实体会

    收藏自http://www.iteye.com/topic/1113732 刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我 ...

随机推荐

  1. 第一周作业—N42-虚怀若谷

    一.Linux发行版描述. Linux发行版主要有三个分支:Slackware.Debian.Redhat: (1) Slackware: SUSE:基于Slackware二次开发的一款Linux,主 ...

  2. MySQL执行计划之EXPLAIN基本解释说明

    一.EXPLAIN使用潜规则 explain + sql语句 例如: EXPLAIN SELECT * FROM `t_user`; 二. 表头字段详解 (1) id-----> 表的读取顺序 ...

  3. div中粘贴图片并上传服务器 div中拖拽图片文件并上传服务器

    应用简介:此文主要是描述如何在前端div中直接ctrl+v 粘贴图片,并上传到服务器,包括拖拽图片文件到div中 应用场景描述:用QQ或者其它切图软件截图,在指定的div中ctrl+v 粘贴并显示,点 ...

  4. Android中实现Activity的启动拦截之----实现360卫士的安装应用界面

    第一.摘要 今天不是周末,但是我已经放假了,所以就开始我们的技术探索之旅,今天我们来讲一下Android中最期待的技术,就是拦截Activity的启动,其实我在去年的时候,就像实现这个技术了,但是因为 ...

  5. 前端每日实战:24# 视频演示如何用纯 CSS 创作出平滑的层叠海浪特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/JvmBdE 可交互视频教程 此视频 ...

  6. Cent OS (一)Cents OS的基本安装

    1.实验环境: VMware Workstation Pro   14 Pro Cent OS 7 系列. 2. 镜像地址传送门: 阿里云开源镜像站:http://mirrors.aliyun.com ...

  7. meta标签 使用说明(http-equiv、refresh、seo)

    meta标签 使用说明(http-equiv.refresh.seo) meta标签,是在head标签里面,一般用做页面描述的.它的内容,用来描述页面一些信息的,如类型.编码.作者.简介等!虽然,它不 ...

  8. English-such as, for example, include and contain

    such as 后接动词,通常用动名词,有时也可用动词原形 for example 后接动词,用动名词 include vt. 包含,包括 后接动词,用动名词 英英: If one thing inc ...

  9. Cygwin访问windows磁盘目录

     http://blog.csdn.net/duguduchong/article/details/7680650 Cygwin访问windows磁盘目录 标签: windows磁盘user平台c 2 ...

  10. stl源码为什么要大量使用typedef?

    SGI源码download,<stl源码剖析>里展示了vector的部分源码: template <class T, class Alloc = alloc> class ve ...