这个程序是这个样子的,首先使用read()函数读取jpg文件转化为byte[],因为某种需要我把byte[]转化为了String,然后再转化为byte[];最后用write()函数保存到test.jpg;可是test.jpg却无法显示;
不知道为什么,谁能帮我解决这个问题import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class Test{
    public static void main(String[] args) {
        test("*.jpg");
    }    public static void test(String path) {        byte[] b1 = read(new File(path));
        String str = new String(b1);
        byte[] b2 = str.getBytes();
        write(new File("test.jpg"), b2);
    }    public static byte[] read(File f) {
        try {
            FileInputStream in = new FileInputStream(f);
            int length = in.available();
            byte[] b = new byte[length];
            in.read(b, 0, length);
            in.close();
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }    public static void write(File f, byte[] b) {
        try {
            FileOutputStream out = new FileOutputStream(f);
            out.write(b);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

解决方案 »

  1.   

    根据传入文件路径,转成字符输出
    =================
    private  String FromFileToString(String filename)
      {
        try
        {
          in=new FileInputStream(filename);
          byte[] buff=new byte[in.available()];
          in.read(buff);
          
          StringBuffer str=new StringBuffer();
          for(int i=0;i<buff.length;i++)
          {
            str.append(buff[i]+" ");
          }
          return str.toString();
        }
        catch(IOException e)
        {
          return null;
        }
        finally
        {
            try
            {
              if(in!=null)
              {
                in.close();
              }
            }
            catch(IOException e)
            {
              System.out.println("close the fileinputstream failed:"+e.getMessage());
            }
        }
      }
      

  2.   

    上面的方法我加了空格做分隔,便于下面用,private void Createfile(String filename,String bytes)
      {    this.Bytes=bytes.split(" ");
        try
        {
          outfile=new FileOutputStream(filename);
          for(int i=0;i<Bytes.length;i++)
          {
            outfile.write(Integer.parseInt(Bytes[i]));
          }
        }
        catch(IOException e)
        {
         
         
        }
        finally
        {
            try
            {
              if(outfile!=null)
              {
                outfile.close();
              }
            }
            catch(IOException e)
            {
              System.out.println("close the write file in the server faild:"+e);
             
            }
        }
      }
      

  3.   

    改一下试试:
     byte[] b2 = str.getBytes("ISO-8859-1");
      

  4.   

    to  Iambest(飞翔的雄鹰) :
    test.jpg还是无法显示;代码:import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class TestBlob {
        public static void main(String[] args) {
            test("*.jpg");
        }    public static void test(String path) {        String str = FromFileToString(path);
            byte[] b = str.getBytes();
            write(new File("test.jpg"), b);
        }    private static String FromFileToString(String filename) {
            FileInputStream in = null;
            try {
                in = new FileInputStream(filename);
                byte[] buff = new byte[in.available()];
                in.read(buff);            StringBuffer str = new StringBuffer();
                for (int i = 0; i < buff.length; i++) {
                    str.append(buff[i] + " ");
                }
                return str.toString();
            } catch (IOException e) {
                return null;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    System.out.println("close the fileinputstream failed:"
                            + e.getMessage());
                }
            }
        }    public static void write(File f, byte[] b) {
            try {
                FileOutputStream out = new FileOutputStream(f);
                out.write(b);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
      

  5.   

    public byte[] getBytes()
       The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required. public String(byte[] bytes)
      The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.
      

  6.   

    to  Iambest(飞翔的雄鹰) :
    你给我的函数都用到了成员变量;
    能不能给一个完整的程序代码啊?
    谢谢
      

  7.   

    to  Iambest(飞翔的雄鹰) :
    谢谢你提供的代码,成功了;
    不过可不可以不使用空格啊,使用其他字符可以吗?比如*,#
      

  8.   

    最好的处理是生成字符串和获取字节数组的时候都指定同一种编码方式
            String str = new String(b1,"ISO-8859-1");
            byte[] b2 = str.getBytes("ISO-8859-1");
    楼主为什么不试一下呢
      

  9.   

    最好的处理是生成字符串和获取字节数组的时候都指定同一种编码方式
            String str = new String(b1,"ISO-8859-1");
            byte[] b2 = str.getBytes("ISO-8859-1");
    楼主为什么不试一下呢
    ___________________________________________________________________
    我刚才粗略的试了一下,好像不幸;
    那我再试试看
      

  10.   

    这个要处理异常的,直接throw掉好了
      

  11.   

    嗯,你的办法也能用,谢谢
    不过你的办法转化为String输出的是乱码,解析时不好控制
    所以我还是会用它的办法,分给你给的少了一下,对不起了
      

  12.   

    public class Test1028 {
        public static void main(String[] args) throws Exception{
            test("output\\1.gif");
            //byte[]b = {97,98};
            //System.out.println(new String(b));
            //b = new String(b).getBytes();
            //System.out.println(b[0]);
        }    public static void test(String path) throws Exception{        byte[] b1 = read(new File(path));
            String str = new String(b1,"ISO-8859-1");
            byte[] b2 = str.getBytes("ISO-8859-1");
            write(new File("2.gif"), b1);
        }    public static byte[] read(File f) {
            try {
                FileInputStream in = new FileInputStream(f);
                int length = in.available();
                byte[] b = new byte[length];
                in.read(b, 0, length);
                in.close();
                return b;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }    public static void write(File f, byte[] b) {
            try {
                FileOutputStream out = new FileOutputStream(f);
                out.write(b);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
      

  13.   

    嗯,我已经测试了,速度也比他的方法快17,18倍,你的速度是47/1000秒,他的速度是781/1000秒;不过你的办法转化为String输出的是乱码,解析时不好控制
    所以我还是会用它的办法