这个程序为什么不能下载中文的文件呢...我也改过编码的,可还是不行,,请各位看看 public void read(String filePath, String fileName) {
try {
String fileName1=fileName;
fileName1= URLEncoder.encode(fileName, "UTF-8");
URL ur = new URL("http://localhost:8080/ftpdowns/" +java.net.URLEncoder.encode(fileName1,"UTF-8"));
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
// OutputStream os = new FileOutputStream(new File(fileName,
// filePath));
OutputStream os = new FileOutputStream(new File(filePath,fileName1));
byte[] bs = new byte[1024];
int len;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();请哪位大虾给改一下啊.小弟现在头痛.到底是哪里有问题
try {
String fileName1=fileName;
fileName1= URLEncoder.encode(fileName, "UTF-8");
URL ur = new URL("http://localhost:8080/ftpdowns/" +java.net.URLEncoder.encode(fileName1,"UTF-8"));
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
// OutputStream os = new FileOutputStream(new File(fileName,
// filePath));
OutputStream os = new FileOutputStream(new File(filePath,fileName1));
byte[] bs = new byte[1024];
int len;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();请哪位大虾给改一下啊.小弟现在头痛.到底是哪里有问题
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class DownloadFile { public static void getSong(String _path, String _savePath) {
String savePath = _savePath;
String path = _path;
int BYTE_SIZE = 1;
int SAVE_SIZE = 1024;
byte[] buff = new byte[BYTE_SIZE]; //每次读的缓存
byte[] save = new byte[SAVE_SIZE]; //保存前缓存
BufferedInputStream bf = null;
FileOutputStream file;
URL url = null;
HttpURLConnection httpUrl;
try {
url = new URL(path);
httpUrl = (HttpURLConnection) url.openConnection();
System.out.println("已经打开连接....");
bf = new BufferedInputStream(httpUrl.getInputStream());
System.out.println("已经获取资源......");
file = new FileOutputStream(savePath);
System.out.println("准备保存到:" + savePath); System.out.println("开始读入......");
int i = 0;
while (bf.read(buff) != -1) { //一个字节一个字节读
save[i] = buff[0];
if (i == SAVE_SIZE - 1) { //达到保存长度时开始保存
file.write(save, 0, SAVE_SIZE);
save = new byte[SAVE_SIZE];
i = 0;
} else {
i++;
}
}
//最后这段如果没达到保存长度,需要把前面的保存下来
if (i > 0) {
file.write(save, 0, i - 1);
}
System.out.println("下载成功!!!");
httpUrl.disconnect();
bf.close();
file.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
DownloadFile
.getSong(
"http://www.vv1111.com.cn/199399/musicliao/938.cc_梦醉西楼-陈瑞.wma",
"D:\\梦.wma");
}}
bf = new BufferedInputStream(httpUrl.getInputStream());
int BYTE_SIZE = 1;
int SAVE_SIZE = 1024;
byte[] buff = new byte[BYTE_SIZE]; //每次读的缓存
byte[] save = new byte[SAVE_SIZE]; //保存前缓存
BufferedInputStream bf = null;
FileOutputStream file;
URL url = null;
HttpURLConnection httpUrl;
try {
url = new URL("http://localhost:8080/ftpdowns/" +fileName);
httpUrl = (HttpURLConnection) url.openConnection();
System.out.println("已经打开连接....");
bf = new BufferedInputStream(httpUrl.getInputStream());
System.out.println("已经获取资源......");
file = new FileOutputStream(filePath);
System.out.println("开始读入......");
int i = 0;
while (bf.read(buff) != -1) { //一个字节一个字节读
save[i] = buff[0];
if (i == SAVE_SIZE - 1) { //达到保存长度时开始保存
file.write(save, 0, SAVE_SIZE);
save = new byte[SAVE_SIZE];
i = 0;
} else {
i++;
}
}
//最后这段如果没达到保存长度,需要把前面的保存下来
if (i > 0) {
file.write(save, 0, i - 1);
}
System.out.println("下载成功!!!");
httpUrl.disconnect();
bf.close();
file.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}fileName传入的文件名是 CDMA网总部新指标体系小时报表$2009-11-29_00-00-00.xls
Tomcat中支持中文文件名的下载在tomcat文件中找到server.xml文件修改如下内容:红色部分为你要加的。另外,如果你是用eclipse或者Jbuilder2007,你不是在tomcat的安装目录,而要在
E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\conf这个里面找到这个server.xml来加这个。我已经解决了,goodluck <Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/>
我把tomcat里的server.xml照你说的改过了.可是还是不行的