我用base64对图片的字节流进行加密,然后存到txt文件中,对txt文件读取,为什么读不出来?
文件中能识别的都能读出来,中间的乱码就读不出来了,为什么??我是用字符流读的
<xml>
<protocol></protocol>
<version>5<version>
<content>fffffffffffffffffffffcxzczx</content>
<media><type>2</type>
<name>1.jpg</name>
<file>
�� JFIF  H H  � ACD Systems Digital Imaging� C  

 $.' ",#(7),01444'9=82<.342� C 2!!22222222222222222222222222222222222222222222222222� �`" �              � B   !1AQa"2q��#BR”$裂34Cbr狃%傫5�&S�              � )      !1A"2Qa#3Bq�    ? 妣M!啴q^喇'6ux":
c!�樠h� 牳4槴o;T%1MLL⑷昸叹弗#欽 �B3J�$舄蕹1XB.] �  駺�<�猢�硱Ч阫Gogr禖�痒*矒�敱H�枏�p爊�藡y 巧�uzmX胹�*�Qd 籁臗F�n您┳'�� E)郣伵.遤�f)繱傮覝畫侊N�S伦敝87$}j�e�孄殬廽%裗苨��
P*坽�幋橍�殾X獱�灛复Z#`骍5�忁�⑦Fp�f懙嬵S廅�5~皱�N6呧�裮h5eL.鞄X�粡壯UP庘Υ�y怇�R僙�b脯镰N�
</file>
</media>
</xml>

解决方案 »

  1.   

    就是对这个xml文件操作,file标签内的乱码就是图片把base64编码之后的,我就想得到这段乱码字符串,不知道为什么io流读到这就不认了,存储我用的utf-8
      

  2.   

      我这个xml是要存视频的,我只是拿图片做个试验,用传统的解析xml会报内存溢出
      

  3.   

    你确定那些是Base64编码后的东西吗?
    正常应该是 /9j/4AAQSkZJRgABAQEAYABgAAD 这样的字符串
    public static void main(String[] args){
     BASE64Encoder encoder = new BASE64Encoder(); 
             try { 
                 File f=new File("D:\\in.jpg"); 
                
                 if(f.exists()) 
                 { 
                  
                     FileInputStream fis = new FileInputStream(f);            
                     byte[] buffer = new byte[(int)f.length()]; 
                     fis.read(buffer); 
                     String s_imageData = encoder.encode(buffer); 
                     Document doc = DocumentHelper.createDocument(); 
                     Element root = doc.addElement("ImageList"); 
                     Element imageID = root.addElement("imageID"); 
                     Element imageInfo = root.addElement("imageInfo"); 
                     Element imageSize = root.addElement("imageSize"); 
                     Element imageData = root.addElement("imageData"); 
                     imageID.addText("01");
                     imageInfo.addText("图片1"); 
                     imageSize.addText(String.valueOf(f.length())); 
                     imageData.addText(s_imageData); 
                     XMLWriter writer = new XMLWriter(new FileOutputStream("d:\\out.xml")); 
                     writer.write(doc); 
                     writer.flush(); 
                     writer.close(); 
                 } 
                 else {
                  System.out.println("找不到要转换的图片文件!");
                 } 
                  
             } catch (FileNotFoundException e) { 
                 e.printStackTrace();
             } catch (IOException e) { 
                 e.printStackTrace(); 
             } 
         }
    这段代码你可以参考一下 需要dom4j
      

  4.   

    估计你写的程序有问题,看看我这个:public class Base64Convert { BASE64Decoder decoder = new BASE64Decoder(); public String ioToBase64(String strPath) throws IOException {
    String fileName = strPath; // 源文件
    String strBase64 = null;
    try {
    InputStream in = new FileInputStream(fileName);
    // in.available()返回文件的字节长度
    byte[] bytes = new byte[in.available()];
    // 将文件中的内容读入到数组中
    in.read(bytes);
    strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
    in.close();
    } catch (FileNotFoundException fe) {
    fe.printStackTrace();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    System.out.println("str:"+strBase64);
    return strBase64;
    } public void base64ToIo(String strBase64,String strPath) throws IOException {
    String string = strBase64;
    String fileName = strPath; // 生成的新文件
    try {
    // 解码,然后将字节转换为文件
    byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    byte[] buffer = new byte[1024];
    FileOutputStream out = new FileOutputStream(fileName);
    int bytesum = 0;
    int byteread = 0;
    while ((byteread = in.read(buffer)) != -1) {
    bytesum += byteread;
    out.write(buffer, 0, byteread); // 文件写操作
    }
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }

    /**
     * 转化为byte数组
     * @param strBase64
     * @return
     * @throws IOException
     */
    public byte[] base64ToIo(String strBase64) throws IOException {

    // 解码,然后将字节转换为文件
    return new BASE64Decoder().decodeBuffer(strBase64); // 将字符串转换为byte数组

    }

    /**
     * 将byte数组转化为字符串
     * @param iobs
     * @return
     * @throws IOException
     */
    public String ioToBase64(byte[] iobs) throws IOException {

    return new BASE64Encoder().encode(iobs); // 将字节流数组转换为字符串;
    }
    }
    测试:public class MyXMLReader {
    public static void main(String arge[]) {  long lasting = System.currentTimeMillis(); 
    Base64Convert con = new Base64Convert();
    try { 


    con.base64ToIo(con.ioToBase64("b.doc"),"c.doc");


    } catch (Exception e) { 
    e.printStackTrace(); 
    }
    finally
    {
    con  = null;
    }
    }
    }
    可以打开c.doc看看内容完全一致,包含图片
      

  5.   

    我不是问的base64 编码问题,是编码后的字符串存到txt文件在读出来就和存之前的字符串不一样,少了一部分
      

  6.   

    public class MyXMLReader {
    public chinese wholesalers
    ugg void main(String arge[]) {long lasting = System.currentTimeMillis();
    Base64Convert con = new Base64Convert();
    try 
      

  7.   

    少了一部分?
    怎么少的?
    base64 编码后都是字母、数字、/的组合,怎么肯能出现乱码呢?你是否自己转码过?
      

  8.   

    String audioPath = path + File.separator + "audio";
    File audioFile = new File(audioPath);
    if(audioFile.isDirectory()){
    File[] files = audioFile.listFiles();
    if(files != null && files.length > 0){
    osw.write("<type>1</type>\r\n"); for(File f:files){
    if(f != null && f.length() > 0){
    osw.write("<name>"+f.getName()+"</name>\r\n");
    osw.write("<file>\r\n"); FileInputStream isr = new FileInputStream(f);
    byte[] bs = new byte[this.LENGTH];

    int flag = 0;
    while((flag = isr.read(bs)) > 0){
    if(flag == this.LENGTH){
    String str = base64.encode(bs);
    osw.write(str+"\r\n");
    }else if(flag > 0){
    byte[] tempBs = new byte[flag];
    for(int i =0;i<flag;i++){
    tempBs[i] = bs[i];
    }
    String str = new BASE64Encoder().encode(tempBs);
    osw.write(str+"\r\n");
    }
    }
    osw.write("</file>\r\n");
    isr.close();
    }
    }
    }
    }
    这是生成xml的程序
    public class MyTest {
    public static void main(String[] args) {
    getStr();
    }
    private static void getStr(){
    File file = new File("update5(2).xml");
    BufferedReader br = null;
    FileOutputStream fos = null;
    boolean flag = false;
    try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
    fos = new FileOutputStream(new File("5.jpg"));
    String str = null;
    String str2 = "";
    while(true){
    str = br.readLine();

    if(str == null){
    break;
    }
    if(str.indexOf("<file>")>=0){
    flag = true;
    continue;
    }
    if(str.indexOf("</file>")>=0){
    flag = false;
    break;
    }
    if(flag){
    str2 += str;

    }
    }

    fos.write(new BASE64Decoder().decodeBuffer(str2));
    fos.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    fos.close();
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    这是解析的程序
      

  9.   

    把内容放到
    <![CDATA[你的内容]]>中间试试看
      

  10.   

    生成xml
    <![CDATA[String audioPath = path + File.separator + "audio";
    File audioFile = new File(audioPath);
    if(audioFile.isDirectory()){
    File[] files = audioFile.listFiles();
    if(files != null && files.length > 0){
    osw.write("<type>1</type>\r\n"); for(File f:files){
    if(f != null && f.length() > 0){
    osw.write("<name>"+f.getName()+"</name>\r\n");
    osw.write("<file>\r\n"); FileInputStream isr = new FileInputStream(f);
    byte[] bs = new byte[this.LENGTH];

    int flag = 0;
    while((flag = isr.read(bs)) > 0){
    if(flag == this.LENGTH){
    String str = base64.encode(bs);
    osw.write(str+"\r\n");
    }else if(flag > 0){
    byte[] tempBs = new byte[flag];
    for(int i =0;i<flag;i++){
    tempBs[i] = bs[i];
    }
    String str = new BASE64Encoder().encode(tempBs);
    osw.write(str+"\r\n");
    }
    }
    osw.write("</file>\r\n");
    isr.close();
    }
    }
    }
    }]]>
    还原成图片
    <![CDATA[public class MyTest {
    public static void main(String[] args) {
    getStr();
    }
    private static void getStr(){
    File file = new File("update5(2).xml");
    BufferedReader br = null;
    FileOutputStream fos = null;
    boolean flag = false;
    try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
    fos = new FileOutputStream(new File("5.jpg"));
    String str = null;
    String str2 = "";
    while(true){
    str = br.readLine();

    if(str == null){
    break;
    }
    if(str.indexOf("<file>")>=0){
    flag = true;
    continue;
    }
    if(str.indexOf("</file>")>=0){
    flag = false;
    break;
    }
    if(flag){
    str2 += str;

    }
    }

    fos.write(new BASE64Decoder().decodeBuffer(str2));
    fos.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    fos.close();
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    ]]>
      

  11.   


    String audioPath = path + File.separator + "audio"; 
    File audioFile = new File(audioPath); 
    if(audioFile.isDirectory()){ 
    File[] files = audioFile.listFiles(); 
    if(files != null && files.length > 0){ 
    osw.write(" <type>1 </type>\r\n"); for(File f:files){ 
    if(f != null && f.length() > 0){ 
    osw.write(" <name>"+f.getName()+" </name>\r\n"); 
    osw.write(" <file>\r\n"); FileInputStream isr = new FileInputStream(f); 
    byte[] bs = new byte[this.LENGTH]; int flag = 0; 
    while((flag = isr.read(bs)) > 0){ 
    if(flag == this.LENGTH){ 
    String str = base64.encode(bs); 
    osw.write(str+"\r\n"); 
    }else if(flag > 0){ 
    byte[] tempBs = new byte[flag]; 
    for(int i =0;i <flag;i++){ 
    tempBs[i] = bs[i]; 

    String str = new BASE64Encoder().encode(tempBs); 
    osw.write(str+"\r\n"); 


    osw.write(" </file>\r\n"); 
    isr.close(); 



    }
      

  12.   

    上边是生成xml的
    这个是解析的public class MyTest {
    public static void main(String[] args) {
    getStr();
    }
    private static void getStr(){
    File file = new File("update5(2).xml");
    BufferedReader br = null;
    FileOutputStream fos = null;
    boolean flag = false;
    try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
    fos = new FileOutputStream(new File("5.jpg"));
    String str = null;
    String str2 = "";
    while(true){
    str = br.readLine();

    if(str == null){
    break;
    }
    if(str.indexOf("<file>")>=0){
    flag = true;
    continue;
    }
    if(str.indexOf("</file>")>=0){
    flag = false;
    break;
    }
    if(flag){
    str2 += str;

    }
    }

    fos.write(new BASE64Decoder().decodeBuffer(str2));
    fos.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    fos.close();
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
      

  13.   

    晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    抓狂到极点!!!!!!!!!!!!!!!!!!!!!!
    不是让你把Java代码放到<![CDATA[]]>里面去!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  14.   

    出现乱码,肯定存入文件时就有问题了,还是用Debug调试一下吧
      

  15.   

    终于找到错了,原来base64编码后的字符串我给加了回车换行,存的时候就出问题了,呵呵,谢谢大家