java文件下载错误提示_java文件下载出错

展开全部

1、response.reset();不应该去reset

2、你参考下我以前写的这段下载pdf的文件吧,稍改下肯定e68a84e8a2ad3231313335323631343130323136353331333332623963能解决楼主的问题了。ServletOutputStream out = res.getOutputStream();

// 用于解决下载后的中文乱码

String contentType = getContentType(fileExt);

// contentType=contentType+";charset=gbk";

res.setContentType(contentType);

System.out.println("显示下载的名称是:-------------------------------"

+ temp);

String contentHeader = "attachment" + ";" + "filename=\""

+ URLEncoder.encode(temp, "UTF-8") + "\"";

res.setHeader("Content-disposition", contentHeader);

System.out.println("contentHeader is =" + contentHeader);

// int MAX_FILE_SIZE = 3 * 1024 * 1024; //此处设置文件最大尺寸,对本程序,不设置即可,3M

int byte_unit = 1024;

// byte[] buffer = new byte[MAX_FILE_SIZE];

byte[] buffer = new byte[byte_unit];// 一次性读取文件的数据长度

int length = 0;

while ((length = is.read(buffer)) != -1) {

out.write(buffer, 0, length);

}

if (is != null)

is.close();

if (out != null) {

out.flush();

out.close();

}

// 获得内容类型

private static String getContentType(String fileExt) {

String contentType = " ";

if (fileExt == null)

contentType = " ";

else if (fileExt.equals("doc"))

contentType = "application/msword ";

else if (fileExt.equals("pdf"))

contentType = "application/pdf";

else if (fileExt.equals("jpg"))

contentType = "image/jpeg";

else if (fileExt.equals("gif"))

contentType = "image/gif ";

else if (fileExt.equals("bmp"))

contentType = "image/bmp ";

else if (fileExt.equals("txt"))

contentType = "text/plain";

else if (fileExt.equals("xls"))

contentType = "application/-excel";

return contentType;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值