本地有张图片A.jpg
转UTF-8 存入A1.jpgA1.jpg  进行转码ASCII   存入A2.jpg   ,但是显示不出图片
//测试2
public void test2(){
 InputStream fis = null;
  FileOutputStream fos = null;
  try {
                  //读取某个文件转换成字符串
  String str = FileUtil.readFileByBete("c:/MMpic/A.jpg", "UTF-8");
 System.out.println(str);
  byte[] b = new byte[1024   *   5];
  fis  = new ByteArrayInputStream(str.getBytes());
   
   fos = new FileOutputStream("c:/MMpic/A1.jpg");
   while(fis.read(b)!=-1){
    fos.write(b);
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   try{
    if(fis!=null) fis.close();
    if(fos!=null) fos.close();
   }catch(Exception e){
    e.printStackTrace();
   }
  }
}
             //读取某个文件转换成字符串

public static String readFileByBete(String filePath,String charset){
FileInputStream br = null;
ByteArrayOutputStream outbyte = null;
try {
 br = new FileInputStream(new File(filePath));
 outbyte = new ByteArrayOutputStream();
   byte[] buffer = new byte[1024]; 
   int length=0;
   while((length=br.read(buffer))!=-1){
   outbyte.write(buffer, 0, length);
   }
}catch(FileNotFoundException e1){
   e1.printStackTrace();
}catch(IOException e2){
   e2.printStackTrace();
}finally{
   try{
    br.close();
   }catch(IOException e3){
    e3.printStackTrace();
   }
}
try {
return new String(outbyte.toByteArray(),charset);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}哪位大神帮俺看看

解决方案 »

  1.   

    图片信息本来就是二进制的,LZ这个转码有什么特殊意义吗?
    你把图片的二进制信息转成字符串,然后再把字符串转成别的编码的二进制信息保存,你觉得这样的图片信息还正确吗?
    fis  = new ByteArrayInputStream(str.getBytes("UTF-8"));//按什么编码转成字符串的,再按什么编码转回去,否则图片信息肯定不一样了,也就是图片就不正确了,还怎么显示
      

  2.   

    我说下 详情情况吧。
    服务器下发一个 图片地址 让客户端(sysbian) 访问这个地址 ,客户端返回的是utf-8 的字符串,我需要对这个字符串操作 转成图片。客户端程序不可以更改了。
      

  3.   

    差不多,我现在就是得到utf-8 的字符串 转编码  jpg文件显示不出来。