- 浏览: 85032 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (97)
- java (23)
- 自定义标签 (3)
- struts (7)
- SSI (0)
- SSH (6)
- IBATIS (14)
- hibernate (2)
- spring (2)
- ssl认证 (1)
- jstl (2)
- jstl中list嵌套map (0)
- ajax (2)
- lucene (2)
- js (5)
- servlet (1)
- jquery (0)
- javascript (7)
- struts1 (1)
- sql (7)
- 公司面试心得 (3)
- jdk (1)
- ERP行业必备的工具 (1)
- 数据库连接池 (1)
- 日志配置问题 (1)
- 呵呵 (0)
- DOJO (1)
- WS (0)
- 工作流JBPM (1)
- 開發工具準備 (1)
- 注解 (0)
- fdsfsdf (0)
- 分页 (0)
- oracle (1)
最新评论
-
kongchuijian:
System.out.println(Arrays.toStr ...
java中数组的问题 -
wyr123789:
你上面已经定义成字节型的为什么还要强转啊?还有你那个'z'-i ...
新手入门 -
ipanel420:
居然是茁壮的同事
SSH2整合+分页+ajax+js校验用户名 -
yemengfan:
楼主能不能把你这个项目发给我啊。我想看看! yemengfa ...
SSH2整合+分页+ajax+js校验用户名
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 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();
}
}
//支持在线打开文件的一种方式
Java代码
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://wujuncheng.iteye.com
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 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();
}
}
//支持在线打开文件的一种方式
Java代码
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://wujuncheng.iteye.com
发表评论
-
JDK、Tomcat、MyEclipse整合
2013-03-13 22:18 0http://wenku.baidu.com/view/222 ... -
dt_003_02
2012-11-16 17:55 0@SuppressWarnings("uncheck ... -
dt_002_03
2012-11-15 19:41 0private static Map<String, L ... -
dt_002_02
2012-11-15 19:22 0package com.star.web.action; i ... -
dt_002_003
2012-11-15 19:13 0public String saveSetting() thr ... -
dt_0002_01
2012-11-15 17:21 0<%@ page language="java ... -
text测试
2012-05-10 18:35 0http://hi.baidu.com/boy86xf/ite ... -
分页测试
2012-05-10 18:34 0package com.chinasoft.remunerat ... -
java入门例子的思考
2011-11-14 13:05 1441public class testjava{ public ... -
IO的操作
2011-03-28 11:38 609一. Input和Output 1. stream代表的是任何 ... -
编码规范
2011-03-25 17:51 719很久以来都在体会着JAVA的风格,现在做一简单陈述,希望大家多 ... -
java中数组的问题
2011-03-17 09:00 894public class ThreeDay { pub ... -
把一个数组中的偶数重新组成一个新数组
2011-03-14 15:08 918挖哈哈!! 刚刚上班,通信联系坏了,公司的人联系不到我,我就 ... -
面试的一个简单程序但是当时没有想起来
2011-03-09 18:38 6771.2.2.4.8.16.32 请问第32位是多少这样的jav ... -
快速排序
2011-03-08 00:03 6221. 采用快速排序,一般 ... -
string类
2011-02-18 01:14 6561、考查"=="的结果 2、String ... -
java集合
2010-10-25 09:24 7451. ArrayList 基于数组方式实现,无容量的限 ... -
java中static private 与 final 详谈
2010-10-19 13:55 1082文章分类:Java编程 1.static表示静态。他是属于类 ... -
java中的对象详谈
2010-10-14 18:29 7322.1 用引用操作对象 (1 ... -
发送邮件
2010-10-14 14:54 752import java.util.HashMap; impor ...
相关推荐
文件上传和下载文件上传和下载文件上传和下载文件上传和下载文件上传和下载文件上传和下载文件上传和下载
上传和下载系统 上传和下载系统上传和下载系统上传和下载系统
经典的java文件上传下载实例。 通过阅读详细的注释,可以比较容易地理解java文件上传和下载的原理和一般的做法。
用java写的文件上传和下载,上传到硬盘物理路劲下,上传后用io读取txt文本文件,修改不能上传和下载是中文名称的bug,简单易用
使用jdk7及以后的文件操作,包含了常用类、常用方法的介绍和文件上传和下载的代码
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
JavaWeb SSM文件的上传和下载(内含数据库),SSM框架 JavaWeb SSM文件的上传和下载(内含数据库),SSM框架
在java代码中实现文件的上传和下载,通过页面的file文件上传到java代码段,获取文件的大小和名字
c++使用libcurl上传和下载资源,封装了库,带lib文件,带nodejs测试服务端
java上传和下载实现有进度条,java上传和下载实现有进度条
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
FtpWebRequest类实现FTP客户端的开发,C#开发,可实现获取服务器文件资源列表并显示出来,可进行文件的上传和下载。
Java文件的上传和下载Java文件的上传和下载Java文件的上传和下载Java文件的上传和下载
如何设置在TIA博途中禁止上传和下载的访问权限?
jsp实现上传和下载,导入项目后可以直接使用。
非常好用的FTP文件上传和下载等管理类,可轻松移植到你的C++程序中,目前已经应用于我们项目中。这个类适用于windows环境下
这个用fileupload组件实现的上传和下载文件的代码,觉得还不错,所以上传上来!
java实现的对各种文件的上传和下载 java实现的对各种文件的上传和下载 java实现的对各种文件的上传和下载 java实现的对各种文件的上传和下载
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例
TFTP多线程上传和下载, 这是Part1. 还有Part2