当前位置:编程学习 > JAVA >>

求解:jsp页面 中文乱码的问题

jsp页面通过js代码判断之后alert出来的中文是乱码,编码都是用的UTF-8,请问要怎么解决? --------------------编程问答-------------------- 如果是写死的话,这个出现就比较诡异了,换个浏览器试试?如果是后台服务器传递过来的话,就是服务器端响应编码没有设正确! --------------------编程问答-------------------- 后台向前台写的话 加上 response.setContentType("text/html;charset=UTF-8");  再不行就decodeURIComponent()  incodeURIComponent() --------------------编程问答-------------------- 支持。。。。。 --------------------编程问答--------------------
引用 1 楼 hly_886 的回复:
如果是写死的话,这个出现就比较诡异了,换个浏览器试试?如果是后台服务器传递过来的话,就是服务器端响应编码没有设正确!


是在页面上写死的 就是在js判断输入的格式是否正确 然后再弹框出来的  --------------------编程问答--------------------
引用 2 楼 dikeboy1234 的回复:
后台向前台写的话 加上 response.setContentType("text/html;charset=UTF-8"); 再不行就decodeURIComponent() incodeURIComponent()


没有到后台去 就是页面上简单的用js判断之后弹框出现的乱码 --------------------编程问答-------------------- 那就在jsp页面加上编码格式 --------------------编程问答--------------------
引用 6 楼 yangting_lisa 的回复:
那就在jsp页面加上编码格式


有加编码格式  这个
 

<%@ page  contentType="text/html;" pageEncoding="UTF-8"%>
--------------------编程问答--------------------
引用 7 楼 minxue_w 的回复:
引用 6 楼 yangting_lisa 的回复:
那就在jsp页面加上编码格式


有加编码格式  这个
 Java code

<%@ page  contentType="text/html;" pageEncoding="UTF-8"%>


+1 --------------------编程问答-------------------- 建议楼主把代码贴出来看一下吧,乱码的原因有很多的 --------------------编程问答-------------------- 是所有的浏览器都这样么?浏览器本身有编码设置的。如果都没有问题,看看tomcat设置的默认编码是不是utf-8,这个设置在server.xml里面 --------------------编程问答--------------------
引用 9 楼 dzr1990 的回复:
建议楼主把代码贴出来看一下吧,乱码的原因有很多的


因为要做文件上传的功能,公司要求用公司内部的组件,就是在这个页面弹框显示的都是乱码

<%@ page contentType="text/html;charset=UTF-8"%>
<%
//String httpUrl = com.talkweb.ei.common.CommonConfig.getInstance().getFileServerHttpDir();
//System.out.println(httpUrl);
boolean attNum = ("1").equals(request.getParameter("attNum"));

String attDiv = request.getParameter("attDiv");
String attInfo  = request.getParameter("attInfo");
String attInfoDetail  = request.getParameter("attInfoDetail");

attDiv  = attDiv==null || attDiv.trim().length()==0 ? "attCreateWord" : attDiv.trim();
attInfo = attInfo==null || attInfo.trim().length()==0 ? "attachment" : attInfo.trim();
attInfoDetail = attInfoDetail==null || attInfoDetail.trim().length()==0 ? "attachmentDetail" : attInfoDetail.trim();

String Time = new java.text.SimpleDateFormat("yyyyMMddhhmmss").format(java.util.Calendar.getInstance().getTime());
%>

<HTML>
<HEAD>
<title>文件上传</title>
<LINK href="../../css/style.css" type="text/css" rel="stylesheet" />
</HEAD>

<body bgColor="#f2f2f2" leftMargin="0" topMargin="0">
<FORM name=form1 METHOD="POST" ACTION="../../servlet/FileUploadServlet" target="hidden_frame" encType="multipart/form-data">
<TABLE cellspacing="0" cellpadding="0" width="100%" border="0">
<!-- 
<TR>
<TD>
<img height="50" src="../../images/win1.jpg" width="12">
</TD>
<TD width="3%" background="../../images/winbg.jpg">
<img height="50" src="../../images/win2.jpg" width="27">
</TD>
<TD width="94%" background="../../images/winbg.jpg">
<img height="50" src="../../images/fjsc.jpg" width="100">
</TD>
<TD class="style2" align="right" width="1%"
background="../../images/winbg.jpg">
<img height="50" src="../../images/win4.jpg" width="1">
</TD>
<TD align="left" width="1%">
<img height="50" src="../../images/win3.jpg" width="9">
</TD>
</TR>
 -->
<TR>
<TD width="1%"> </TD>
<TD colspan="3">
<TABLE height="100%" cellspacing="0" cellpadding="0" width="100%"
border="0">
<TR>
<TD valign="top" width="1%" bgcolor="#f1f1f1">
<TABLE height="100%" cellspacing="0" cellpadding="0" width="1"
bgcolor="#0e4e96" border="0">
<TR>
<TD width="1"></TD>
</TR>
</TABLE>
</TD>
<TD align="center" width="97%" bgcolor="#f1f1f1">
<TABLE id="Table1" cellspacing="1" cellpadding="1" width="98%"
align="center" bgcolor="#f2f2f2" border="0">
<TR id="trDisplay1">
<TD class="tr3" width="20%">
 文     件:
</TD>
<TD>
<input type="FILE" name="FILE1" size="25">
<input id="btnup" type="button" value="上传" onclick="submit_upload();myupload(0)">
</TD>
</TR>

<TR id="trDisplay3" height="45">
<TD class="tr3">
  附件列表:
</TD>
<TD>
<SPAN id="Label1" class="tr3" style="display:block"><FONT color="red">
 *选中列表框中文件名,点击删除,则此文档不上传。<br> *点击确定后,不能再次上传;点击关闭后,能再次上传。
</FONT>
</SPAN>
</TD>
</TR>
<TR>
<TD class="ourfont1"><FONT face="宋体"></FONT></TD>
<TD align="left">
<select style="WIDTH: 80%" multiple="multiple" size="5" name=fileList></select>
</TD>
</TR>
<TR>
<TD align="center" colspan="2">
<input type="button" name="uploadFile" value="确定" id="BtSave" class="but50" onclick="submitUpload();myconfirm();">
<input type="button" name="button2" value="删除" id="button2" class="but50" onclick="delMyFileInfo();delSelected(this.form.fileList);">
<input type="button" name="Button3" value="关闭" id="Button3" class="but50" onClick="javascript:window.close();" />
<input type="hidden" name="HTTPDOWNLOAD" width=0 value=''>
<input width=0 type="hidden" name="uploadFileHead" value="">
<input width=0 type="hidden" name="uploadFileAdd" value="">
<input width=0 type="hidden" name="allFile" value="">
<input type="hidden" id="myFileInfo" value="">
</TD>
</TR>
</TABLE>
<BR>
</TD>
<TD valign="top" align="right" width="1%" bgcolor="#f1f1f1">
<TABLE height="100%" cellspacing="0" cellpadding="0" width="1"
bgcolor="#0e4e96" border="0">
<TR>
<TD width="1"></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR bgcolor="#0e4e96">
<TD colspan="3" height="1"></TD>
</TR>
</TABLE>
</TD>
<TD></TD>
</TR>
</TABLE>
</FORM>
<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>

<script>
var attdiv  = "<%=attDiv %>";
var attInfo = "<%=attInfo %>";
var attInfoDetail = "<%=attInfoDetail%>";

window.onload =function (){funUploadFile('','');};
function myalert(msg){
window.alert(msg);
}
function myupload(i){
if(i==0){
document.getElementById('Label2').style.display="block";
document.getElementById('Label1').style.display="none";
}
if(i==1){
document.getElementById('Label1').style.display="block";
document.getElementById('Label2').style.display="none";
}
}    
function myconfirm(){
opener.document.getElementById('lnkupload').style.display="none";

}
function delMyFileInfo(){
var filedir = document.all.item('fileList').value;
var myFileInfo = document.getElementById("myFileInfo").value;
if(filedir!=null&&filedir!=""&&myFileInfo!=null&&myFileInfo!=""){
var files = myFileInfo.split("#");
myFileInfo = "";
for(var i=0;i<files.length;i++){
var file = files[i];
if(file==null||file==""){
continue;
}
var infos = file.split("@");
var url = infos[1];
if(url!=filedir){
myFileInfo = myFileInfo+"#"+file;
}
}
document.getElementById("myFileInfo").value = myFileInfo;
window.opener.document.getElementById(attInfoDetail).value = myFileInfo;
}
}

function isAttNum(){
var attNum = '<%=attNum%>';
if(attNum=='true'&&document.all.item('fileList').options.length>0) {
alert("一次只允许添加一个附件!");
return true;
} else {
return false;
}
}

function submit_upload(){

if(isAttNum()) return false;

var file_name = document.all.FILE1.value;
try{
var len = file_name.length;
var ixg = 0;
var fileName = "";

if(file_name.indexOf("\\")!=-1)
ixg = file_name.lastIndexOf("\\");
else 
ixg = file_name.lastIndexOf("/");

fileName = file_name.substr(ixg+1);

for (var i=0; i<document.all.item('fileList').options.length;i++){
var f_name = document.all.item('fileList').options(i).text;
if(fileName==f_name){
alert("该附件已经上传,请选择其他附件!");
return;
       }
     }

if(file_name.indexOf("<")>=0 || file_name.indexOf(">")>=0 || file_name.indexOf("#")>=0 || 
   file_name.indexOf("&")>=0 || file_name.indexOf(";")>=0 || file_name.indexOf("+")>=0 ||
   file_name.indexOf(",")>=0 || file_name.indexOf("\'")>=0 || file_name.indexOf("\"")>=0 ){
alert('请不要上传带“<、>、#、&、;、+、,、\'、\"”名字的文件,可以输入全角的,请修改名字再上传!');
}else if(file_name.indexOf("  ")>=0){
alert("上传的附件名不能包含两个或两个以上相邻的空格字符!");
}else if(file_name.indexOf("正式文件.doc")!=-1 ){
alert("上传的附件中不能包含“正式文件.doc”关键字样!");
}else if(len-ixg>100){
alert("上传的附件名字太长!");
}else{
document.form1.submit();
}
}catch(e){}
}


--------------------编程问答--------------------

function funUploadFile(strAtt1,strAttInfo1){
var strAtt;
var strfile ;

try {
    if (strAtt1=="null" || strAtt1=="") {
strAtt = window.opener.document.getElementById(attInfo).value;       
      
      if (strAtt!=null && strAtt!="") {
       while(strAtt.indexOf("@")>0) { 
          strfile = strAtt.substring(0,strAtt.indexOf("@"));
                if (strAtt.indexOf("\\")!=-1) {
          strfileText = strfile.substring(strfile.lastIndexOf("\\")+1,strfile.length);
          } else {
          strfileText = strfile.substring(strfile.lastIndexOf("/")+1,strfile.length);
          }
          var optionTmp =document.createElement('OPTION');
              optionTmp.text=strfileText;
              optionTmp.value=strfile;
              document.all.fileList.options.add(optionTmp); 
          strAtt = strAtt.substring(strAtt.indexOf("@")+1, strAtt.length);
          }
document.all.allFile.value=window.opener.document.getElementById(attInfo).value;      
      }
   }
   if(strAttInfo1 == null || strAttInfo1 == "" || strAttInfo1 == "null"){
   document.getElementById("myFileInfo").value=window.opener.document.getElementById(attInfoDetail).value;
   }
} catch(e) {}

   document.all.uploadFileHead.value = window.opener.document.all.uploadFileHead.value.replace(new RegExp("\\\\","gm"),"/");
document.all.uploadFileAdd.value = window.opener.document.all.uploadFileAdd.value.replace(new RegExp("\\\\","gm"),"/") + attdiv + "/<%=Time %>";

// servlet传回来的值
if (strAtt1!="null") {
while(strAtt1.indexOf("@")>0) { 
          strfile = strAtt1.substring(0,strAtt1.indexOf("@"));          
          strfileText = strfile.substring(strfile.lastIndexOf("/")+1,strfile.length)
          var optionTmp =document.createElement('OPTION');
              optionTmp.text=strfileText;
              optionTmp.value=strfile;
              document.all.fileList.options.add(optionTmp); 
          strAtt1 = strAtt1.substring (strAtt1.indexOf("@")+1,strAtt1.length);
     } 
     document.all.allFile.value="<%=request.getParameter("attachment")%>";
   }
   if(strAttInfo1!=null && strAttInfo1!=""){
   var myFileInfo = document.getElementById("myFileInfo").value;
document.getElementById("myFileInfo").value = myFileInfo + "#" +strAttInfo1;
   }
   if(document.all.fileList.options.length>0){
   document.getElementById("btnup").disabled='false';
   }else{
   document.getElementById("btnup").removeAttribute("disabled");
   }
    }

function delSelected(tbox) {

var i,j=0,c;
var tLength = tbox.options.length;
var arrTbox = new Array();
var arrLookup = new Array();
for (i=0; i<tLength; i++) {
if(tbox.options[i].selected == false){
    arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[j] = tbox.options[i].text;
j++;
}
}
tbox.length=0;
for(c=0; c<arrTbox.length; c++){
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}
document.all.allFile.value = "";

var HTTPDOWNLOAD=document.all.HTTPDOWNLOAD.value;
for(k=0;k<tbox.options.length;k++){
    if (document.all.allFile.value != ""){
    document.all.allFile.value += "@"+tbox.options[k].value;
   }else{
   document.all.allFile.value += tbox.options[k].value;
   }
}

var fileAdd="";
  var strInerHtml ="";
  var stratt ="";
       for (var i = 0; i< document.all.item('fileList').options.length;i++){
var optionTmp = window.opener.document.createElement('OPTION');
var fileText = document.all.item('fileList').options(i).text;
fileText = fileText.substring(fileText.lastIndexOf("\\")+1);
       optionTmp.text=fileText;
       var test = document.all.item('fileList').options(i).value;
       stratt += test+"@";
       while (test.lastIndexOf("\\")!=-1) {
       test = test.replace("\\","\/");
       }
      
       optionTmp.value=document.all.item('fileList').options(i).value; 
       // window.opener.document.all.attachments.options.add(optionTmp); 
       // window.opener.document.all.attachments.options[i].selected="true"; 

  strInerHtml+=(i+1)+".<A href=\"javascript:downPopUp('"+test+"');\">"+fileText+"</A>   ";
       //strInerHtml+=(i+1)+".<A href=\"../upload/openfile.jsp?path="+HTTPDOWNLOAD+test+"\" target=_blank>"+fileText+"</A>   ";
       }
      
       //for(var i = 0; i< window.opener.document.all.item('attachments').options.length;i++){
       // window.opener.document.all.attachments.options[i].selected="true"; 
       //}
       if(document.all.fileList.options.length<=0){
   document.getElementById("btnup").removeAttribute("disabled");
   }
       window.opener.document.getElementById(attdiv).innerHTML=strInerHtml;
       window.opener.document.getElementById(attInfo).value=stratt;
       try{window.opener.document.all.faMaterial.value=stratt;}catch(e){}
}

function submitUpload(){

if(document.all.allFile.value=="") {
     alert("您没有上传附件!");
     return false;
  }else{
  var fileAdd="";
      var strInerHtml ="";
      var stratt ="";
    
      var HTTPDOWNLOAD=document.all.HTTPDOWNLOAD.value;
       for (var i = 0; i< document.all.item('fileList').options.length;i++){
var optionTmp = window.opener.document.createElement('OPTION');
var fileText = document.all.item('fileList').options(i).text;
fileText = fileText.substring(fileText.lastIndexOf("\\")+1);
       optionTmp.text=fileText;
       var test =document.all.item('fileList').options(i).value;
       stratt+=test+"@";
       while (test.lastIndexOf("\\")!=-1) {
       test = test.replace("\\","\/");
       }
      
       optionTmp.value=document.all.item('fileList').options(i).value; 
       //window.opener.document.all.attachments.options.add(optionTmp); 
       //window.opener.document.all.attachments.options[i].selected="true"; 
     
       strInerHtml+="<A href=\"javascript:downPopUp('"+test+"');\">"+fileText+"</A>   ";
       //if (fileText!="")
       //strInerHtml+=(i+1)+".<A href=\"../upload/openfile.jsp?path="+HTTPDOWNLOAD+test+"\" >"+fileText+"</A>   ";
       }
      
       //for(var i = 0; i< window.opener.document.all.item('attachments').options.length;i++){
       //  window.opener.document.all.attachments.options[i].selected="true"; 
       //}
       try{
       window.opener.document.getElementById(attdiv).innerHTML=strInerHtml;
       window.opener.document.getElementById(attInfo).value=stratt;
       window.opener.document.getElementById(attInfoDetail).value = document.getElementById("myFileInfo").value;
       //try{window.opener.document.all.faMaterial.value=stratt;}catch(e){}
       //try{window.opener.document.all.materialAttach.value=stratt;}catch(e){}
       try{
       if('<%=attNum%>'=='true') window.opener.document.getElementById(attdiv).innerHTML= "  "+strInerHtml.substring(2);
       //window.opener.document.all.materialAttach.value=stratt;
       //var result = document.all.item('fileList').options(0).text;
       //if(result.indexOf(".")!=-1)result = result.substring(0,result.indexOf("."));
       //window.opener.document.all.materialName.value=result;
       }catch(e){}
       }catch(e){}
      
       window.close();
   }
}
</script>
</BODY>
</HTML>


--------------------编程问答-------------------- 看的头大,关键代码在哪里 --------------------编程问答--------------------
引用 13 楼 softroad 的回复:
看的头大,关键代码在哪里




function submit_upload(){

if(isAttNum()) return false;

var file_name = document.all.FILE1.value;
try{
var len = file_name.length;
var ixg = 0;
var fileName = "";

if(file_name.indexOf("\\")!=-1)
ixg = file_name.lastIndexOf("\\");
else 
ixg = file_name.lastIndexOf("/");

fileName = file_name.substr(ixg+1);

for (var i=0; i<document.all.item('fileList').options.length;i++){
var f_name = document.all.item('fileList').options(i).text;
if(fileName==f_name){
alert("该附件已经上传,请选择其他附件!");
return;
       }
     }

if(file_name.indexOf("<")>=0 || file_name.indexOf(">")>=0 || file_name.indexOf("#")>=0 || 
   file_name.indexOf("&")>=0 || file_name.indexOf(";")>=0 || file_name.indexOf("+")>=0 ||
   file_name.indexOf(",")>=0 || file_name.indexOf("\'")>=0 || file_name.indexOf("\"")>=0 ){
alert('请不要上传带“<、>、#、&、;、+、,、\'、\"”名字的文件,可以输入全角的,请修改名字再上传!');
}else if(file_name.indexOf("  ")>=0){
alert("上传的附件名不能包含两个或两个以上相邻的空格字符!");
}else if(file_name.indexOf("正式文件.doc")!=-1 ){
alert("上传的附件中不能包含“正式文件.doc”关键字样!");
}else if(len-ixg>100){
alert("上传的附件名字太长!");
}else{
document.form1.submit();
}
}catch(e){}
}

--------------------编程问答-------------------- 打开浏览器 ---> 菜单栏---> 查看 --->字符编码--->设置成GBK/如果不行,改成UTF-8


赶紧给分 --------------------编程问答--------------------
引用 15 楼 cyl713 的回复:
打开浏览器 ---> 菜单栏---> 查看 --->字符编码--->设置成GBK/如果不行,改成UTF-8


赶紧给分


浏览器设施没有错,一直都是UTF-8的 而且 别的jsp页面弹框不会乱码 就这个会  --------------------编程问答-------------------- 你是说类似alert("上传的附件名不能包含两个或两个以上相邻的空格字符!");这样的alert会乱码? --------------------编程问答--------------------
引用 17 楼 softroad 的回复:
你是说类似alert("上传的附件名不能包含两个或两个以上相邻的空格字符!");这样的alert会乱码?


恩恩  --------------------编程问答-------------------- 把你这个jsp文件本身的编码改成UTF-8,如果是eclipse的话就右键,属性,可以修改。
要么换成window.alert试试。不会是把alert重写了吧。 --------------------编程问答--------------------
引用 19 楼 softroad 的回复:
把你这个jsp文件本身的编码改成UTF-8,如果是eclipse的话就右键,属性,可以修改。
要么换成window.alert试试。不会是把alert重写了吧。


那个的话早就换掉了的 
alert重写是? --------------------编程问答-------------------- 要不就把文件copy出来,用ue,或者记事本打开看看,是不是那些中文已经是乱码了。或者你把那些中文删除自己重新输入下。 --------------------编程问答-------------------- 再解决不了,就是遇见鬼了。 --------------------编程问答--------------------
引用 21 楼 softroad 的回复:
要不就把文件copy出来,用ue,或者记事本打开看看,是不是那些中文已经是乱码了。或者你把那些中文删除自己重新输入下。



记事本打开的不是乱码 而且 开始的时候担心组件是从另外一个项目copy下来或者down下来的。两个机器的文件编码不一样。所以你先删除alert里面的字 用自己机器重新写的 

--------------------编程问答-------------------- alert中直接写中文也是弹出的乱码?
--------------------编程问答-------------------- 帮顶学习下,乱码问题确实比较烦躁……祝lz好运 --------------------编程问答-------------------- 我见意吗:
把它放入一个变量里,然后用转换:
  response.setContentType("text/xml; charset=UTF-8"); 

str=java.net.URLDecoder.decode(str,"GB2312");
str=new String(str.getBytes("ISO-8859-1"));
out.print(str);

-------------------
Javascript escape(), encodeURI(), encodeURIComponent() 
--------------------------
这几种方法,肯定可以的。
试试吧。。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,