org.zkoss.zul.Filedownload.java 源码
/* 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 源码的更多相关文章
- 如何阅读Java源码 阅读java的真实体会
刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比 ...
- Android反编译(一)之反编译JAVA源码
Android反编译(一) 之反编译JAVA源码 [目录] 1.工具 2.反编译步骤 3.实例 4.装X技巧 1.工具 1).dex反编译JAR工具 dex2jar http://code.go ...
- 如何阅读Java源码
刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动.源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧, ...
- Java 源码学习线路————_先JDK工具包集合_再core包,也就是String、StringBuffer等_Java IO类库
http://www.iteye.com/topic/1113732 原则网址 Java源码初接触 如果你进行过一年左右的开发,喜欢用eclipse的debug功能.好了,你现在就有阅读源码的技术基础 ...
- Programming a Spider in Java 源码帖
Programming a Spider in Java 源码帖 Listing 1: Finding the bad links (CheckLinks.java) import java.awt. ...
- 解密随机数生成器(二)——从java源码看线性同余算法
Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...
- Java--Eclipse关联Java源码
打开Eclipse,Window->Preferences->Java 点Edit按钮后弹出: 点Source Attachment后弹出: 选择Java安装路径下的src.zip文件即可 ...
- 使用JDT.AST解析java源码
在做java源码的静态代码审计时,最基础的就是对java文件进行解析,从而获取到此java文件的相关信息: 在java文件中所存在的东西很多,很复杂,难以用相关的正则表达式去一一匹配.但是,eclip ...
- [收藏] Java源码阅读的真实体会
收藏自http://www.iteye.com/topic/1113732 刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我 ...
随机推荐
- 常见HTTP错误代码
了解更多HTTP错误代码 一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态 ...
- php str_pad()函数 语法
php str_pad()函数 语法 str_pad()函数怎么用? php str_pad()函数用于把字符串填充到指定长度,语法是str_pad(string,length,pad_string, ...
- 深入理解js——非构造函数的继承
原文来自阮一峰日志(http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.ht ...
- [CSP-S模拟测试]:虎(DFS+贪心)
题目传送门(内部题15) 输入格式 第一行一个整数$n$,代表点数接下来$n-1$行,每行三个数$x,y,z$,代表点$i$与$x$之间有一条边,若$y$为$0$代表初始为白色,否则为黑色,若$z$为 ...
- [CSP-S模拟测试]:Star Way To Heaven(最小生成树Prim)
题目描述 小$w$伤心的走上了$Star\ way\ to\ heaven$. 到天堂的道路是一个笛卡尔坐标系上一个$n\times m$的长方形通道(顶点在$(0,0)$和$(n,m)$),小$w$ ...
- python练习题之随机生成验证码
#引用random模块下的randint项目#定义验证码函数.定义一个空字符串变量,分三种情况,随机产生的大写字母,随机产生的小写字母,随机产生的数字.然后#每一次执行哪一种情况,条件也是随机的,就是 ...
- Maven之自定义pom类型的基础项目
摘要:在当前的软件开发场景中,大都是通过maven管理项目,而如果使用maven的话,其实也会有很多问题,比如项目中依赖的版本管理就是一个很头疼的事,如果一个项目中有很多人同时开发那么这就很可能造成大 ...
- Redis入门很简单之七【使用Jedis实现客户端Sharding】
Redis入门很简单之七[使用Jedis实现客户端Sharding] 博客分类: NoSQL/Redis/MongoDB redisjedisspringsharding分片 <一>. 背 ...
- Linux随笔 - linux 多个会话同时执行命令后history记录不全的解决方案【转载】
基本认识linux默认配置是当打开一个shell终端后,执行的所有命令均不会写入到~/.bash_history文件中,只有当前用户退出后才会写入,这期间发生的所有命令其它终端是感知不到的. 问题场景 ...
- Windows 08 R2_创建AD DS域服务(图文详解)
目录 目录 Active Directory概念 创建第一个AD域控制器 搭建DNS服务器 使用Windows窗口程序创建AD域控制器 AD与LDAP的关系 使用Powershell来创建ADDS域控 ...