java实现文件下载 调用IE自带的下载工具来完成下载
2013-05-07 14:01
609 查看
/**
* @author
* May 7, 2013 1:56:03 PM
* @param path 下载文件的全路径(包括文件名)
* @param response
* @throws FileNullException 自定义异常,当前文件不存在的时候抛出此异常
* @throws IOException
*/
public static void download(String path, HttpServletResponse response) throws FileNullException, IOException {
OutputStream toClient = null;
InputStream bis = null;
FileInputStream fis = null;
// path是指欲下载的文件的路径。
File file = new File(path);
//判断当前文件是否存在,若是不存在则跑出异常
if(!file.exists()) {
throw new FileNullException("下载失败,该文件不存在,可能已经被删除");
}
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
byte[] buffer = new byte[bis.available()];
fis.read(buffer);
fis.close();
bis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.flush();
toClient.write(buffer);
toClient.close();
}
注意事项:我这里并没有抓取异常信息,而是将异常信息抛向了调用方(action)中,在action中进行了除了。FileNullException自定义异常,主要是用来抛出文件不存在的信息,用于在页面提示用户。当然也可以有其他处理方式,可自行实现。
由于csdn上传资源功能有问题,导致经常无法上传,顾我将不上传源代码。有意者可以留言索要。
* @author
* May 7, 2013 1:56:03 PM
* @param path 下载文件的全路径(包括文件名)
* @param response
* @throws FileNullException 自定义异常,当前文件不存在的时候抛出此异常
* @throws IOException
*/
public static void download(String path, HttpServletResponse response) throws FileNullException, IOException {
OutputStream toClient = null;
InputStream bis = null;
FileInputStream fis = null;
// path是指欲下载的文件的路径。
File file = new File(path);
//判断当前文件是否存在,若是不存在则跑出异常
if(!file.exists()) {
throw new FileNullException("下载失败,该文件不存在,可能已经被删除");
}
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
byte[] buffer = new byte[bis.available()];
fis.read(buffer);
fis.close();
bis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.flush();
toClient.write(buffer);
toClient.close();
}
注意事项:我这里并没有抓取异常信息,而是将异常信息抛向了调用方(action)中,在action中进行了除了。FileNullException自定义异常,主要是用来抛出文件不存在的信息,用于在页面提示用户。当然也可以有其他处理方式,可自行实现。
由于csdn上传资源功能有问题,导致经常无法上传,顾我将不上传源代码。有意者可以留言索要。
相关文章推荐
- oracle 调用Java 实现ftp文件的上传和下载 问题
- IE下使用js调用系统activex实现文件下载
- java图片开源框架tesseract调用OCR实现图片文件识别代码下载
- 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
- JAVA 程序实现文件到linux服务的上传与下载
- java实现文件压缩下载----压缩下载zip
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- java实现ftp下载文件【转】
- java 获取网络文件获取流下载,支持IE火狐 直接下载而不直接打开
- 需求(Java语言实现):对存有图片URL的txt文件进行逐行读取,自行下载txt文件中的图片到指定文件夹中
- java 实现下载htttp文件的简便办法 FileUtils IOUtils
- Java实现HTTP文件下载(转)
- java输出流实现文件下载
- OSS实现多文件多线程的断点下载(java)
- 用Java实现FTP批量大文件上传下载(三)
- Java实现的文件下载
- Java实现HTTP文件下载
- java 下载文件 兼容 ie 火狐 google 浏览器 ,解决下载文件乱码问题
- Java实现FTP批量大文件上传下载篇2
- java实现文件下载功能,自动弹出保存窗口