- 浏览: 334297 次
- 性别:
- 来自: 济南
文章分类
最新评论
-
u013810758:
[flash=200,200][url][img][list] ...
poi 解析excel 03与07不兼容问题 -
u013810758:
我也是这么做的 但是还是有问题啊 感觉07版的或03版的内容 ...
poi 解析excel 03与07不兼容问题 -
sezi915:
我觉得如果用异常来判断Excel版本的话 用org.apach ...
poi 解析excel 03与07不兼容问题 -
他大姨妈:
思路很好,楼主脑子比较好使
poi 解析excel 03与07不兼容问题 -
zhenlong_qu:
谢谢博主的这篇文章,不得不说,博主的思路真不错,按照这个思路, ...
poi 解析excel 03与07不兼容问题
java下载文件的种方式文章分类:Java编程
转自:http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html
Java文件下载的几种方式
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
// 下载本地文件
String fileName = "Operator.doc".toString(); // 文件的默认保存名
// 读到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
// 下载网络文件
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//支持在线打开文件的一种方式 public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
转自:http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html
Java文件下载的几种方式
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
// 下载本地文件
String fileName = "Operator.doc".toString(); // 文件的默认保存名
// 读到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
// 下载网络文件
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//支持在线打开文件的一种方式 public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
发表评论
-
http断点续传
2014-07-24 13:31 905Http文件下载的普通模式就不多说了,断点下载与普通模式不一 ... -
修改Tomcat的运行内存 & JSP中查看当前的内存使用状况
2014-03-24 16:00 993一、设置JVM内存设置 1. 设置JVM内存的参数有 ... -
Eclipse编码格式修改
2014-03-21 09:13 770如果要使插件开发应 ... -
标题叠加在图片上面的几种显示方式
2014-02-24 10:55 1472很多新改版的网站都会在图片显示上下一些文章,比如加 ... -
java druid淘宝数据库连接池数据库监听
2013-12-26 15:40 13451java程序很大一部分 要操作数据库,为了提高性能操作数据库 ... -
memcached- tomcat配置详解
2013-12-04 18:08 1405msmconfigurationmemcached-se ... -
Oracle中将小数转换成字符丢零.截取小数.除数为零解决法
2013-11-26 14:33 82271.小数转换成字符往往会丢失前面的零.解决方法_例:to_c ... -
java 垃圾回收配置
2013-10-22 14:16 4432一、JVM内存模型及垃 ... -
nginx配置详解
2013-10-18 14:45 1039user nginx ; #用户 ... -
oracle 列转行
2013-10-11 21:15 16274业务中做报表,需要将一列列数据汇总成一行,然后汇总,如下: ... -
osgi 注册servlet 和资源
2013-10-08 10:02 1656在Activator类中 public void s ... -
正则表达式 匹配url
2013-07-09 14:27 1526正则表达式 (http|ftp|https):\ ... -
ZooKeeper 集群配置
2013-07-05 14:40 1167ZooKeeper 是一个分布式开源框架,提供了协调 ... -
jdk logger 配置实用宝典
2013-06-18 15:45 1789日志输出是所有系统必备的,很多开发人员可能因为常常使用log ... -
CSS实现兼容性的渐变背景(gradient)效果
2013-06-05 14:32 977CSS实现兼容性的渐变背 ... -
jinterop获取windows事件
2013-05-27 13:42 1798Java代码 package com ... -
jcifs windows主机之间进行网络文件共享
2013-05-20 18:05 1141CIFS (Common Internet File ... -
java调用windows的wmi
2013-05-20 14:34 2039100%纯java调用windows的wmi获取监控数据 ... -
java调用windows的wmi
2013-05-20 14:32 3966100%纯java调用windows的wmi获取监控数据 ... -
tomcat 域名
2013-03-14 14:44 18851. 下载安装版本的Tomcat(或者压缩版的,只不过要自己配 ...
相关推荐
Java文件下载的几种方式,供大家参考!!!
java文件下载的几种方式
Java文件下载的几种方式,是很方便的,找了半天找到了,共享一下
Java实现文件下载功能,以WORD文档方式展现给用户看。
Java实现文件与图片的上传下载---三种方式 方法1:JSP+Servlet; 方法2:smartUpload; 方法3:FileUpload;
Java代码,根据URL方式下载单个文件或者图片,根据文件大小进行分批启动多线程下载!
JAVA通过SMB的方式下载、上传远程Linux服务器的文件
使用流下载文件。java response 下载文件方法。
Java作业:实现客户端下载文件的功能(只需要实现下载某一个文件的功能就可以)
这是随手写的一个利用线程池下载文件的小玩具,如果刚学完线程池不知道怎么使用的,可以拿来借鉴一下,互相学习。里面或许有些代码不太合理,但是作为练手项目还是可以的。 适用人群: 刚学完多线程和线程池,以及...
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
java 流的方式下载 上传 批量下载zip
NULL 博文链接:https://thoreau.iteye.com/blog/765827
java语言ftp方式上传和下载。
本工程用于研究Java Web文件下载 本工程编码方式:UTF-8 说明:本工程下载功能解决了使用IE11下载文件时所出现的乱码问题
java.util.jar 提供读写 JAR (Java ARchive) 文件格式的类,该格式基于具有可选清单文件的标准 ZIP 文件格式。 java.util.logging 提供 JavaTM 2 平台核心日志工具的类和接口。 java.util.prefs 此包允许应用程序...
Java实现HTTP连接与浏览,Java源码下载,输入html文件地址或网址,显示页面和HTML源文件,一步步的实现过程请下载本实例的Java源码,代码中包括丰富的注释,对学习有帮助。 Java实现的FTP连接与数据浏览程序 1个...
已经测试 完全没问题 大可以满足你的简单需求 修改液很容易 里面有详细的注释说明
java通过ftp三种方式 上传 下载文件: 1. 通过sun :sun.net.ftp.FtpClient 2. 通过apache:org.apache.commons.net.ftp.FTPClient 3. 通过edtftp: com.enterprisedt.net.ftp.FileTransferClient