java下载文件实战_文件下载工具类 DownLoadUtil 实战

package com.cloud.mina.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

/***

* 下载文件工具类

*/

public class DownLoadUtil {

private static final String CONTENT_TYPE = "application/x-msdownload";

/**

* 下载文件

* @param response HttpServletResponse

* @param title 下载时候的文件名称

* @param file 要下载的文件对象

* @return true 下载成功    false 下载失败

*/

public static boolean downLoadFile(HttpServletResponse response,String filename,File file){

boolean result = true;

FileInputStream input = null;

OutputStream out = null;

try {

//设置response的编码方式

response.setContentType(CONTENT_TYPE);

//写明要下载的文件的大小

response.setContentLength((int)file.length());

//设置附加文件名

response.setHeader("Content-Disposition","attachment;filename=\""+new String

(filename.getBytes("UTF-8"),"iso-8859-1")+"\"");

//读出文件到i/o流

input =new FileInputStream(file);

//从response对象中得到输出流,准备下载

out = response.getOutputStream();

if(input!=null && out!=null){    // 判断输入或输出是否准备好

int temp = 0 ;

try{

while((temp=input.read())!=-1){    // 开始拷贝

out.write(temp) ;    // 边读边写

}

}catch(IOException e){

e.printStackTrace() ;

result = false;

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

result = false;

} catch (IOException e) {

result = false;

e.printStackTrace();

} finally{

if(input!=null){

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

result = false;

}

}

if(out!=null){

try {

out.flush();

out.close();//关闭输出流

} catch (IOException e) {

e.printStackTrace();

result = false;

}

}

}

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值