我写了个从tomcat里下载文件的程序.昨天不能下载名字为中文名的文件.我就把server.xml里的编码改了一下..现在可以下了.但是下下来的文件名给变成了乱码.其它的都没有变..这是下载前的文件名(CDMA网总部新指标体系小时报表$2009-11-29_06-00-00.xls)这是下载后的文件名(CDMA%E7%BD%91%E6%80%BB%E9%83%A8%E6%96%B0%E6%8C%87%E6%A0%87%E4%BD%93%E7%B3%BB%E5%B0%8F%E6%97%B6%E6%8A%A5%E8%A1%A8%242009-11-29_04-00-00.xls)..哪位给看看啊.该怎么改.
这是下载程序.我把程序里的fileName转码成UTF-8就不能下载了.报filenotfoundexception.
这是下载程序.我把程序里的fileName转码成UTF-8就不能下载了.报filenotfoundexception.
将%E7%BD%91其实就是0xE70xBD0x91,也就是UTF-8编码的中文'罗'
自己写个编码转换,或上网搜下URL编码的转换,写个方法就OK了
你把中文的文件名 用下面的方法处理一下 然后在试试。
下面 是把utf-8 明文 转为 %23%xx…… 的形式 /**
* Utf8URL编码
* @param s
*/
public String Utf8URLencode(String text) {
StringBuffer result = new StringBuffer(); for (int i = 0; i < text.length()/2; i++) { char c = text.charAt(i);
if (c >= 0 && c <= 255) {
result.append(c);
}else { byte[] b = new byte[0];
try {
b =text.toString().getBytes("UTF-8");
}catch (Exception ex) {
} for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
result.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return result.toString();
}
//将传过来的参数 转化成以GB2312编码形式如"交警返回是%BD%BB%BE%AF"
public String GBURLencode(String text) {
StringBuffer result = new StringBuffer(); byte[] b = new byte[0];
try {
b = text.getBytes("GB2312");
}
catch (Exception ex) {
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
result.append("%" + Integer.toHexString(k).toUpperCase());
} return result.toString();
}
try {
fileName= URLEncoder.encode(fileName, "UTF-8");
URL ur = new URL("http://localhost:8080/ftpdowns/" +fileName);
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(new File(filePath,fileName));
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();
}刚才忘记发下载程序了..我把fileName编码会报错的
* 解码URL串
*
* @param url
* 待解码的URL串
* @return 解码后的字符串
*/
public static String decode(String url) {
char[] chars = url.toCharArray();
int cursor = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(baos);
while (cursor < chars.length) {
int curr = chars[cursor++];
if (curr == '%') {
StringBuffer tmp = new StringBuffer();
char cr = chars[cursor++];
char lf = chars[cursor++];
tmp.append(cr).append(lf);
Integer result = Integer.valueOf(tmp.toString(), 16);
curr = result.intValue();
}
dos.writeByte(curr);
}
dos.flush();
baos.flush();
byte[] data = baos.toByteArray();
// String text = UTF8.decode(data, 0, data.length);
String text = new String(data, "utf-8");
return text;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dos != null)
dos.close();
if (baos != null)
baos.close();
} catch (Exception e) {
}
}
return null;
}
解码后的文字为:CDMA网总部新指标体系小时报表$2009-11-29_04-00-00.xls
package com.dtb.comment.download;import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.File;
import java.net.URL;public class DownLoadFile {
public static boolean downLoadFile(String source, String path) {
String filename = "";
if (path.indexOf(".") == -1) {
filename = source.substring(source.lastIndexOf("/") + 1, source
.length());
if (!path.endsWith("/"))
path = path + "/";
} File file = new File(path + filename);
if (file.exists()) {
System.out.println("has a same file in : \n" + path + filename
+ "\n and now it will be overwrite");
} else
System.out.println("文件存放路径为:" + path + filename);
try {
URL sourceurl = new URL(source);
InputStream is = sourceurl.openStream();
FileOutputStream fos = new FileOutputStream(path + filename);
byte[] bytes = new byte[1];
int c;
while ((c = is.read(bytes)) != -1) {
fos.write(bytes, 0, c);
}
is.close();
fos.close();
} catch (Exception e) {
return false;
}
return true;
} public static void main(String[] args) {
System.out.println(downLoadFile(
"http://www.lcread.com/bookPage/11476/1050414.js",
"E:/temp/"));
}
}能不能把你下载的地址发上来,我没有遇到过这样的情况.