導航:首頁 > 編程語言 > java返回值亂碼

java返回值亂碼

發布時間:2023-05-28 20:13:54

㈠ 使用java連接linux,執行shell命令返回值有亂碼,怎麼解決

packagecom.pasier.xxx.util;

importjava.io.IOException;
importjava.io.InputStream;
importjava.nio.charset.Charset;

importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;

importch.ethz.ssh2.ChannelCondition;
importch.ethz.ssh2.Connection;
importch.ethz.ssh2.Session;
importch.ethz.ssh2.StreamGobbler;

publicclassRmtShellExecutor{

privatestaticfinalLoggerLOG=LoggerFactory.getLogger(RmtShellExecutor.class);

privateConnectionconn;
privateStringip;
privateStringusr;
privateStringpsword;
privateStringcharset=Charset.defaultCharset().toString();

privatestaticfinalintTIME_OUT=1000*5*60;

publicRmtShellExecutor(Stringip,Stringusr,Stringps){
this.ip=ip;
this.usr=usr;
this.psword=ps;
}

privatebooleanlogin()throwsIOException{
conn=newConnection(ip);
conn.connect();
returnconn.authenticateWithPassword(usr,psword);
}

publicStringexec(Stringcmds)throwsIOException{
InputStreamstdOut=null;
InputStreamstdErr=null;
StringoutStr="";
StringoutErr="";
intret=-1;

try{
if(login()){
Sessionsession=conn.openSession();
session.execCommand(cmds);
stdOut=newStreamGobbler(session.getStdout());
outStr=processStream(stdOut,charset);
LOG.info("caijl:[INFO]outStr="+outStr);
stdErr=newStreamGobbler(session.getStderr());
outErr=processStream(stdErr,charset);
LOG.info("caijl:[INFO]outErr="+outErr);
session.waitForCondition(ChannelCondition.EXIT_STATUS,TIME_OUT);
ret=session.getExitStatus();

}else{
LOG.error("caijl:[INFO]ssh2loginfailure:"+ip);
thrownewIOException("SSH2_ERR");
}

}finally{
if(conn!=null){
conn.close();
}
if(stdOut!=null)
stdOut.close();
if(stdErr!=null)
stdErr.close();
}

returnoutStr;
}

privateStringprocessStream(InputStreamin,Stringcharset)throwsIOException{
byte[]buf=newbyte[1024];
StringBuildersb=newStringBuilder();
while(in.read(buf)!=-1){
sb.append(newString(buf,charset));
}
returnsb.toString();
}

publicstaticvoidmain(String[]args){

Stringusr="root";
Stringpassword="12345";
StringserverIP="11.22.33.xx";
StringshPath="/root/ab.sh";

RmtShellExecutorexe=newRmtShellExecutor(serverIP,usr,password);

StringoutInf;

try{
outInf=exe.exec("sh"+shPath+"xn");
System.out.println("outInf="+outInf);
}catch(IOExceptione){
e.printStackTrace();
}
}

}

㈡ 新浪JAVA API返回的JSON數據裡面中文亂碼

這是unicode編碼,不是亂碼,你進行Unicode轉碼就出來了

給你寫個方法轉碼,將版unicode傳遞進去返回字元串

	publicStringconvert(StringutfString){
權StringBuildersb=newStringBuilder();
inti=-1;
intpos=0;

while((i=utfString.indexOf("\u",pos))!=-1){
sb.append(utfString.substring(pos,i));
if(i+5<utfString.length()){
pos=i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2,i+6),16));
}
}

returnsb.toString();
}

㈢ 如何解決 Java 構造 HTTP 請求 POST 的返回值是亂碼

對返回結果使用適當的編碼格式,比如

newInputStreamReader(urlConn.getInputStream(),"gbk");//設置編碼

㈣ java輸出結果亂碼

這不是亂碼,而是int[] y對象的內存地址值。你的y仍然是一個一維數組對象,所以才會出現這種情況。如果想要輸出所有的元素的話可以這樣:
for(int y[]:x){
for(int i:y){
System.out.println(i);
}
}

當然你也可以重寫y數組的toString()方法,在toString裡面可以定義你想要輸出的格式, Object裡面的toString方法默認列印出來 是類名+@+hashCode值

㈤ java 介面返回出現亂碼

你彎猜可也在Web.xml裡面配置一下這個埋野型
<!--解決中文亂碼 -->
<filter>
<filter-name>SpringEncoding</filter-name>
<filter-class>脊遲org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>SpringEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--解決中文亂碼 -->

㈥ java的HttpClient請求,返回的數據部分亂碼,請大俠指點呀!

可以把你的這段代腔仔碼簡化一下

ins=method.getResponseBodyAsStream();
byte[]b=咐蘆newbyte[1024];
int伍簡汪r_len=0;
while((r_len=ins.read(b))>0)
{
result.append(newString(b,0,r_len,method.getResponseCharSet()));
}

替換為:

byte[]ba=method.getResponseBody();
Stringstr=newString(ba,"UTF-8"); //得到指定編碼格式的字元串

這里的str就是你的返回值。這段代碼我使用過,可以解決亂碼問題

㈦ Java代碼出現了亂碼怎麼辦

運行java帶有中文的代碼就出現亂碼,解決方式如下:

設置整個java工程的編碼格式為utf-8,如下圖:

設置html的編碼格式為utf-8

㈧ java get 請求 返回值亂碼

您好,提問者:
如果包含中文,不建議使用get方式,可採用post提交方式。

//可以採用加密、解碼的方式進行加碼提交,例如:
//加碼
Stringstr=java.net.URLEncoder.encode("中國","UTF-8");
//解碼
Stringjiema=java.net.URLDecoder.decode(str,"UTF-8");
閱讀全文

與java返回值亂碼相關的資料

熱點內容
彩視製作教程 瀏覽:766
聖墟在哪個App看免費 瀏覽:395
網路哪些不能玩 瀏覽:868
probe315使用教程 瀏覽:646
數字電位器程序 瀏覽:198
c代碼整理 瀏覽:104
網路營銷具有什麼優勢 瀏覽:378
右下角網路連接不顯示寬頻連接 瀏覽:940
ps修改tif文件 瀏覽:580
預防醫學如何轉行做大數據 瀏覽:234
pdf文件變藍 瀏覽:309
怎麼在pdf文件上面用k寶簽名 瀏覽:213
如何知道表格里數據後面有空格 瀏覽:720
gee引擎更新系統找不到指定文件 瀏覽:802
貝殼網的數據刪除了如何找回 瀏覽:509
華為榮耀6x怎麼切換網路 瀏覽:418
手機里的pdf文件在哪放 瀏覽:889
java版貪吃蛇畢業論文 瀏覽:989
微信公共號郵箱 瀏覽:415
圖片寬度代碼 瀏覽:460

友情鏈接