zlping 2009-05-22 15:44
浏览 351
已采纳

如何将图片二进制--字符串--二进制流?

需求是这样的:我要把一个图片用webservice发布出去,流程 图片---》二进制---》字符串---》调用webserivce接口---》字符串---》二进制---》图片。不能直接发布二进制接口,因为客户端不支持。

现在遇到的问题是二进制 转字符串 再转成二进制后就和原来的不一样了。

我写的代码[code="java"]
File file=new File("d:\test.jpg");
InputStream input =new FileInputStream(file);
byte[] temp = new byte[input.available()];

tem.read(temp);
tem.close();
String outstr = new String(temp);
byte[] outbyte = outstr.getBytes();
File outfile=new File("d:\testout.jpg");
FileOutputStream fos = new FileOutputStream(outfile);
fos.write(outbyte);
fos.close();
[/code]

这样转化后[code="java"]
String outstr = new String(temp);
byte[] outbyte = outstr.getBytes();
[/code]
temp 和 outbyte就不一样了,试了各种编码还是不行
请问怎么解决
ps:如果是字符串来回转都没事,图片不行

  • 写回答

3条回答 默认 最新

  • zwei1021 2009-05-22 20:33
    关注

    [code="java"]
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    File inFile = new File("d:\1.gif");
    File outFile = new File("d:\2.gif");
    FileInputStream fis = new FileInputStream(inFile);
    byte [] bytes=new byte[fis.available()];
    fis.read(bytes);
    String str = new String(bytes,"iso8859-1");
    FileOutputStream fos = new FileOutputStream(outFile);
    fos.write(str.getBytes("iso8859-1"));
    fos.close();
    fis.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    

    [/code]

    这段代码可以.注意要使用iso8859-1编码格式,不然有可能造成字节丢失.
    发送方:String str = new String(bytes,"iso8859-1");
    接受方:str.getBytes("iso8859-1")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?