java 下载文件内容为空_java实现下载文件,如果文件名为中文,下载后内容为空,求解释!...

BufferedInputStreambis=null;BufferedOutputStreambos=null;HttpURLConnectionconn=null;path=path.replaceAll("%20","");try{URLurl=newURL(path);conn=(HttpURLConnection)url.ope...

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

HttpURLConnection conn=null;

path=path.replaceAll("%20", " ");

try {

URL url=new URL(path);

conn=(HttpURLConnection) url.openConnection();

conn.setConnectTimeout(6000); //设置连接超时时间6s

conn.connect();

//response.reset();

response.setContentType("application/x-msdownload;");

response.addHeader("Content-Disposition","attachment;filename=\""+new String(fileName.getBytes("gb2312"),"iso8859-1")+"\"");

response.setContentType("application/octet-stream");

bis=new BufferedInputStream(conn.getInputStream());

byte[] buffer = new byte[bis.available()];

int len;

bos = new BufferedOutputStream(response.getOutputStream());//new FileOutputStream(fileName);

while((len = bis.read(buffer))!=-1){

bos.write(buffer,0,len);

}

bos.flush();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{ //关闭各类流

if(conn!=null){

conn.disconnect();

}

if(bis!=null){

try {

bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(bos!=null){

try {

bos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

当下载中文文件名的文件时,程序报错,如图,所示为出错的地方。

展开

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值