java.io.utfdataformatexception:字符串太长

时间:2012-12-09 11:04:03

标签: java client-server utf

我在将标题发送到java服务器时获得了标题中的异常

以下是代码:

       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       img.compress(Bitmap.CompressFormat.PNG, 100, stream);
       byte[] byteArray = stream.toByteArray();

       String imageDataString = new String(Base64.encodeBase64(byteArray)); 
       System.out.println(imageDataString);

       dataOutputStream.writeUTF(imageDataString);
       dataOutputStream.flush();

img是位图文件。

任何帮助都将受到高度赞赏!

1 个答案:

答案 0 :(得分:1)

@Sarram遵循吹制链接中的代码,我在soap请求中发送图像以及base64String形式的其他数据我将其转换为文件

blow是代码的引用

Writing decoded base64 byte array as image file

我正在使用这个很酷的解码器import sun.misc.BASE64Decoder; 服务器端可以这样做

        String filePath = "/destination/temp/file_name.jpg";
        File imageFile = new File(filePath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(imageFile);//create file
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        BASE64Decoder decoder = new BASE64Decoder();//create decodeer object
        byte[] decodedBytes = null;
        try {
            decodedBytes = decoder.decodeBuffer(imageFileBase64);//decode base64 string that you are sending from clinet side 
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            fos.write(decodedBytes);//write the decoded string on file and you have ur image at server side
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }