我使用了过滤器,能够处理页面的中文问题,但上传文件时,我想得到文件名字可是中文名字总是乱码,部分代码:
ServletInputStream sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("filename=")) != -1) {
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
System.out.println(s.substring(s.lastIndexOf("\\")+1,k);
}
}文件名字时英文没有问题,中文就是乱码,谢谢帮忙看看
ServletInputStream sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("filename=")) != -1) {
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
System.out.println(s.substring(s.lastIndexOf("\\")+1,k);
}
}文件名字时英文没有问题,中文就是乱码,谢谢帮忙看看
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("filename=")) != -1) {
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
System.out.println(s.substring(s.lastIndexOf("\\")+1,k);
}
}[/code]
你可以选用Apache的文件上传插件,这个不会.还有你传送文件名时,想办法带上编码格式.
另:楼主的代码貌似对这个问题没什么用
String fileName = "中文文件名";
String formatFileName = new String(fileName.getBytes("GBK"),"ISO8859_1");
throws ServletException, IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("filename=")) != -1) {
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k+1);
System.out.println(new String(s.substring(s.lastIndexOf("\\")+1,k).getBytes("UTF-8"),"ISO-8859-1"));
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("filename=")) != -1) {
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k+1);
System.out.println(new String(s.substring(s.lastIndexOf("\\")+1,k).getBytes("UTF-8"),"ISO-8859-1"));
}
System.out.println(new String(s.substring(s.lastIndexOf("\\")+1,k).getBytes("iso-8859-1"),"UTF-8"));
乱码为:
乱码为:
new String(s.substring(s.lastIndexOf("\\")+1,k).getBytes("ISO-8859-1"),"UTF-8")乱码为:
a.txt
{filename=a.txt
......
}
{}中间的表示文件的内容。
你将上传的文件用记事本默认的格式,看看还乱码不?
已经正常了.我想弄个jsp页面可是怎么调用fckeditor的上传呢?
String fileName = "中文文件名";
String formatFileName = new String(fileName.getBytes("ISO8859_1"),"GBK");