/* 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. 4412 移植mpu9250尝试

    4412的板子IO都是1.8v的.只有I2C6是用了电平转换到了3.3v.所以我准备使用I2C6来驱动mpu9250 一.首先去掉占用的模块 menuconfig中去掉触摸的驱动 Device Dri ...

  2. Python中的时间模块和日期模块

    Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间 ...

  3. (转)VirtualBox下安装CentOS7系统

    转:https://www.cnblogs.com/hihtml5/p/8217062.html 本文假定你已经知道如何安装VirtualBox虚拟机软件,并且已经安装好了. 首先我们需要准备好cen ...

  4. vs 2019 create new project 创建新项目

    下面的place solution and project in the same directory 不需要勾选

  5. iOS图片压缩问题

    对于压缩的处理我给出的建议是 先判断 图片的大小,如果是本地图片最好用nsfilemanager 来判断 .如果不能用这个判断的话 就只能先将图片转成data,然后再判断了. 图片转成data 当然就 ...

  6. Day 56 jquery

    一 .事件委托实例 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...

  7. PromQL

    PromQL (Prometheus Query Language) 是 Prometheus 自己开发的数据查询 DSL 语言,语言表现力非常丰富,内置函数很多,在日常数据可视化以及rule 告警中 ...

  8. anaconda 安装2个python环境 亲测

    本机环境: anaconda3,pyhon3.7.4 配置第2个python环境,安装python3.6 > conda create --name tensorflow python=3.6 ...

  9. HDU 3466 Proud Merchants(01背包)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  10. Dubbo 系列(05-1)服务发布

    目录 Dubbo 系列(05-1)服务发布 Spring Cloud Alibaba 系列目录 - Dubbo 篇 1. 背景介绍 1.1 服务暴露整体机制 2. 源码分析 2.1 前置工作 2.2 ...