异常Invalid byte 1 of 1-byte UTF-8 sequence

小王来巡山 2011-05-10 04:18:09
是这样的,我加密xml后之后,然后对xml进行解密,但是解密后只有一个字是乱码,我觉得这个问题很奇怪,所以发帖请大家帮忙来看看,代码如下:

package test12;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.jdom.Element;

@SuppressWarnings(value = "all")
public class MySwing extends JFrame {

HashMap<String, Integer> hash;
MySwing ms;
JFileChooser fileChooser = new JFileChooser();
JFileChooser fileChooser2 = new JFileChooser();
//为了方便事件驱动,将控件做成全局变量
JLabel label = null;

JTextField jtfpath = new JTextField();

JButton jbtnfilecon = new JButton("导入");
JButton jbtnjiami = new JButton("加密");
JButton jbtnjiemi = new JButton("解密");

public MySwing(){
//设置手动布局
this.setLayout(null);
this.setTitle("脚本加密");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 350, 350);
//初始化窗体内的组件
initComponent();
//放在最后
this.setVisible(true);
}

public static void parseXml(Element e,PrintStream ps,ParseDes pd) throws Exception{
ps.println(pack(e.getName()));
List<Element> list = e.getChildren();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
if(list == null||list.size() == 0) {
if(!"".equals(e.getValue())&&!" ".equals(e.getValue()))
ps.println(pd.encrypt(e.getValue()));
}else{
for (Element e2 : list) {
parseXml(e2,ps,pd);
}
}
ps.println(packDesc(e.getName()));
}

public static String pack(String line ){
return "<"+line+">";
}

public static String packDesc(String line ){
return "</"+line+">";
}

void initComponent(){
//得到放组件的内容窗格
Container contentPane = this.getContentPane();

//手动布局时一定要自己设定位置和大小

jtfpath.setBounds(10, 50, 200, 25);

//初始不可编辑定时删除时间
jtfpath.setEnabled(false);

//给按钮添加动作监听器

jbtnfilecon.setBounds(260, 50, 60, 25);
jbtnfilecon.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
doOpenjiamifile();
}

private void doOpenjiamifile() {
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("打开文件夹");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int ret = fileChooser.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
//文件夹路径
System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
jtfpath.setText(fileChooser.getSelectedFile().getAbsolutePath());
//用上面这个方法设置textfield
}
}

});

jbtnjiami.setBounds(60, 220, 60, 40);
jbtnjiami.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
try {
String test = "";
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(
fileChooser.getSelectedFile()
.getAbsolutePath())));
ParseDes des = new ParseDes();//自定义密钥
FileOutputStream fos = new FileOutputStream(new File(
"script_des.txt"));
while ((test = br.readLine()) != null) {
fos.write((des.encrypt(test) + System.getProperty("line.separator")).getBytes());
fos.flush();
}
br.close();
fos.close();
} catch (Exception e) {
// TODO: handle exception
}
JOptionPane.showMessageDialog(null, "加密OK!");
}
});

jbtnjiemi.setBounds(160, 220, 60, 40);
jbtnjiemi.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
try {
String test = "";
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileChooser.getSelectedFile().getAbsolutePath())));
ParseDes des = new ParseDes();//自定义密钥
FileOutputStream fos = new FileOutputStream(new File("script_jiemi.xml"));
while ((test = br.readLine()) != null) {
fos.write((new String(des.decrypt(test).getBytes(),"utf-8")+System.getProperty("line.separator")).getBytes());
fos.flush();
}
br.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "解密OK!");
}
});



//给单选框做监听
/* 用一个按钮组对象包容一组单选按钮 */
/* 生成一个新的动作监听器对象,备用 */
/* 为各单选按钮添加动作监听器 */
/* 将单选按钮添加到按钮组中 */
//初始设置修改,删除按钮不可用

contentPane.add(jtfpath);
contentPane.add(jbtnfilecon);
contentPane.add(jbtnjiami);
contentPane.add(jbtnjiemi);

}

protected void doExit(ActionEvent e) {
System.exit(1);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
MySwing msSwing = new MySwing();
msSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
msSwing.setVisible(true);
}
});
}

}

XML内容如下:::
...全文
353 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxb346571110 2011-06-07
  • 打赏
  • 举报
回复
字符转义出错,debug
小王来巡山 2011-05-10
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="gb2312"?>
<CollStations>
<CollStation>
<SystemName>zhwdx07</SystemName>
<SystemPassword>123456</SystemPassword>
<endpointURL>http://10.25.116.242:8080/axis/SendDataBySoap_java.jws?wsdl</endpointURL>
<CheckTime>5000</CheckTime>
<Mode>RunMode</Mode>
<Type>descrypt</Type>
<Scripts>
<ScriptType>Connect</ScriptType>
<CommandID>0x43002100</CommandID>
<Ip>10.25.26.5</Ip>
<Port>22</Port>
<User>weihu</User>
<Password>Chlry20!</Password>
<RtnChar>></RtnChar>
<CommandPrompt> </CommandPrompt>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002200</CommandID>
<CommandString>df -k</CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
<AnaScripts>
<CommandID>0x43002200</CommandID>
<CRunID>0x43002200</CRunID>
<retrievename>10.25.26.5-/dev/sda2 磁盘空间使用率</retrievename>
<superscript>/dev/sda2</superscript>
<suffix> </suffix>
<delimiter> </delimiter>
<index>3</index>
<Option>SELECT</Option>
</AnaScripts>
<AnaScripts>
<CommandID>0x43002300</CommandID>
<CRunID>0x43002200</CRunID>
<retrievename>10.25.26.5-tmpfs 磁盘空间使用情况</retrievename>
<superscript>tmpfs </superscript>
<suffix> </suffix>
<delimiter> </delimiter>
<index>3</index>
<Option>SELECT</Option>
</AnaScripts>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002500</CommandID>
<CommandString>vmstat 2 </CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002600</CommandID>
<CommandString>ps aux |grep smc</CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString>03</StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002700</CommandID>
<CommandString>cd /home/smc </CommandString>
<CommandPrompt> </CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002800</CommandID>
<CommandString>find . -size +30000000c </CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002900</CommandID>
<CommandString>df </CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002a00</CommandID>
<CommandString>ls</CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002b00</CommandID>
<CommandString>hostname </CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>send</ScriptType>
<CommandID>0x43002c00</CommandID>
<CommandString>more /etc/hosts </CommandString>
<CommandPrompt>></CommandPrompt>
<ContinuePrompt> </ContinuePrompt>
<ContinueString> </ContinueString>
<StopString> </StopString>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>Wait</ScriptType>
<TimeOut>15000</TimeOut>
</Scripts>
<Scripts>
<ScriptType>Disconnect</ScriptType>
</Scripts>
</CollStation>
<CollStation>
<SystemName>zhwdx08</SystemName>
<SystemPassword>123456</SystemPassword>
<endpointURL>http://10.25.116.242:8080/axis/SendDataBySoap_java.jws?wsdl</endpointURL>
<CheckTime>5000</CheckTime>
<Mode>RunMode</Mode>
<Type>descrypt</Type>
<Scripts>
<ScriptType>Connect</ScriptType>
<CommandID>0x43002d00</CommandID>
<Ip>10.25.26.6</Ip>
<Port>22</Port>
<User>weihu</User>
<Password>Chlry20!</Password>
<RtnChar>></RtnChar>
<CommandPrompt> </CommandPrompt>
<TimeOut>15000</TimeOut>
</Scripts>
</CollStation>
</CollStations>
请大家帮我看看,谢谢了,最好能把代码帮我改改,很急

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧