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),不禁又有一种激动. 源码阅读,我 ...
随机推荐
- MariaDB强势席卷DB-Engines榜单后续,与阿里云达成全球独家战略合作
2018年10月份,DB-Engines 发布了月全球数据库排名,排名前三的一如既往还是Oracle.MySQL.Microsoft SQL Server.排名是重要指标,同时增长率的重要性也同样备受 ...
- 接触python的第1天:测试hello world
在python3.8的平台可以输入了hello world, ide还能当做计算器 >>> print("hello world") hello world &g ...
- POJ 3348 Cows (凸包模板+凸包面积)
Description Your friend to the south is interested in building fences and turning plowshares into sw ...
- Linux 虚拟机通过NAT模式访问外网
1.配置本地VM8地址 2.配置虚拟机NAT网卡:设置VM8网卡地址和Linux主机相同网段地址,网关随便设置 3.编译网卡文件 vim /etc/sysconfig/network-scri ...
- 北风设计模式课程---20、UML类图介绍
北风设计模式课程---20.UML类图介绍 一.总结 一句话总结: 不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例 设计模式都是对生活的抽象,比如用户获得装备,我可以先装备工厂先生 ...
- sudo su 和sudo -s的区别
sudo su 和 sudo -s都是切换到root用户,不同的是: sudo su 环境用的是目标用户(root)的环境 sudo -s 环境用的是当前用户本身的环境
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Xeon
http://en.wikipedia.org/wiki/Comparison_of_Intel_processors Processor Series Nomenclature Code Name ...
- 基础复习之HTML (doctype、标签语义化)
这段时间找实习看的眼花缭乱的,然后也被拒得落花流水,啊哈哈-还是写博客好玩儿-嘿嘿,下面正题 一.doctype 标准模式 (Full Standards Mode) 接近标准模式 (Almost S ...
- 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...
- upc组队赛15 Supreme Number【打表】
Supreme Number 题目链接 题目描述 A prime number (or a prime) is a natural number greater than 1 that cannot ...