中文文件下载的问题,网上的咨询和答疑已经很多,我原来处理下载的代码如下:
response.setheader("content-disposition", "attachment; filename=" + java.net.urlencoder.encode(filename, "utf-8"));
下载的程序里有了这句,一般在ie6的下载提示框上将正确显示文件的名字,无论是简体中文,还是日文。不过当时确实没有仔细测试文件名很长的中文文件名。先如今经过仔细测试,发现文字只要超过17个字,就不能下载了。经过好一番google和反复测试,总算对这个问题有了系统的认识
public partial class filedownload : system.web.ui.page
{
//提供下载的文件,不编码的话文件名会乱码
private string filename = httpcontext.current.server.urlencode("规范.rar");
private string filepath = httpcontext.current.server.mappath("规范.rar");
//使用transmiffile下载文件
protected void btndl1_click(object sender, eventargs e)
{
fileinfo info = new fileinfo(filepath);
long filesize = info.length;
response.clear();
response.contenttype = "application/x-zip-compressed";
response.addheader("content-disposition", "attachment;filename="+ filename);
//不指明content-length用flush的话不会显示下载进度
response.addheader("content-length", filesize.tostring());
response.transmitfile(filepath, 0, filesize);
response.flush();
response.close();
}//使用writefile下载文件
protected void btndl2_click(object sender, eventargs e)
{
fileinfo info = new fileinfo(filepath);
long filesize = info.length;
response.clear();
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachement;filename=" + filename);
//指定文件大小
response.addheader("content-length", filesize.tostring());
response.writefile(filepath, 0, filesize);
response.flush();
response.close();
}//使用outputstream.write分块下载文件
protected void btndl3_click(object sender, eventargs e)
{
//指定块大小
long chunksize = 102400;
//建立一个100k的缓冲区
byte[] buffer = new byte[chunksize];
//已读的字节数
long datatoread = 0;
filestream stream = null;
try
{
//打开文件
stream = new filestream(filepath, filemode.open, fileaccess.read, fileshare.read);
datatoread = stream.length;//添加http头
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachement;filename=" + filename);
response.addheader("content-length", datatoread.tostring());while (datatoread > 0)
{
if (response.isclientconnected)
{
int length = stream.read(buffer, 0, convert.toint32(chunksize));
response.outputstream.write(buffer, 0, length);
response.flush();
response.clear();
datatoread -= length;
}
else
{
//防止client失去连接
datatoread = -1;
}
}
}
catch (exception ex)
{
response.write("error:" + ex.message);
}
finally
{
if (stream != null)
{
stream.close();
}
response.close();
}
}//使用binarywrite下载文件,大文件效率不行
protected void btndl4_click(object sender, eventargs e)
{
filestream stream = null;
try
{
//读文件,大文件一次读入会占用大量内存
stream = new filestream(filepath, filemode.open, fileaccess.read, fileshare.read);
byte[] bytes = new byte[stream.length];
stream.read(bytes, 0, bytes.length);
stream.close();//添加http头
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachement;filename=" + filename);
response.addheader("content-length", bytes.length.tostring());
response.binarywrite(bytes);
response.flush();
}
catch (exception ex)
{
response.write("error:" + ex.message);
}
finally
{
if (stream != null)
{
stream.close();
}
response.close();
}
}
//使用binarywrite分块下载文件
protected void btndl5_click(object sender, eventargs e)
{
//指定区块和缓冲区
long chunksize = 102400;
byte[] buffer = new byte[chunksize];
filestream stream = null;
long datatoread = 0;
try
{
stream = new filestream(filepath, filemode.open, fileaccess.read, fileshare.read);
datatoread = stream.length;
//添加http头
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachement;filename=" + filename);
response.addheader("content-length", datatoread.tostring());while (datatoread > 0)
{
if (response.isclientconnected)
{
int length = stream.read(buffer, 0, convert.toint32(chunksize));
response.binarywrite(buffer);
response.flush();
response.clear();datatoread -= length;
}
else
{
datatoread = -1;
}
}}
catch(exception ex)
{
response.write("error:" + ex.message);
}
finally
{
if (stream != null)
{
stream.close();
}
response.close();
}
}
}