我要做的页面上提供一个按钮,用来下载某URL地址的文档资源。形式如http://192.168.4.122:8001/upfiles/docs/test.xls".下载的时候不包含中文的文件可以正常下载,包含中文的文件名则不能下载。
代码:
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition","attachment; filename=\"" +"test.xls");
//String destUrl = "http://192.168.4.160:8001/upfiles/docs/11537988019986-12test.xls";//英文文件可以下载
String destUrl = "http://192.168.4.160:8001/upfiles/docs/11537988019986-12报送表.xls";//含中文,报错
try{
int BUFFER_SIZE =8192;
OutputStream sout =null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0; //建立链接
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
sout = response.getOutputStream();
while ( (size = bis.read(buf)) != -1)
sout.write(buf, 0, size);
bis.close();
httpUrl.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
代码:
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition","attachment; filename=\"" +"test.xls");
//String destUrl = "http://192.168.4.160:8001/upfiles/docs/11537988019986-12test.xls";//英文文件可以下载
String destUrl = "http://192.168.4.160:8001/upfiles/docs/11537988019986-12报送表.xls";//含中文,报错
try{
int BUFFER_SIZE =8192;
OutputStream sout =null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0; //建立链接
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
sout = response.getOutputStream();
while ( (size = bis.read(buf)) != -1)
sout.write(buf, 0, size);
bis.close();
httpUrl.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
java.io.FileNotFoundException: http://192.168.4.160:8001/upfiles/docs/1153798801
9986-12报送表.xls
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:1147)
后面还有一大堆。
为什么找不到文件啊怎么解决?
java.net.URLEncoder.encode(destUrl ,utf-8);
好的,我先试试。
java.io.FileNotFoundException: http://192.168.4.160:8001/upfiles/docs/1153798801
9986-12%E6%8A%A5%E9%80%81%E8%A1%A8.xls
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:1147)
等高手来给你解决