-1

画像を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ビットマップ ファイルはどこにありますか。

どんな助けでも大歓迎です!

4

1 に答える 1

1

@Sarramはブローリンクのコードに従ってください。私はsoapリクエストで画像をbase64Stringの形式で他のデータと一緒に送信し、それをファイルに変換していました

打撃はコードの参照です

デコードされた base64 バイト配列を画像ファイルとして書き込む

私はこのクールなデコーダーを使用して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();
        }
于 2012-12-09T11:25:36.540 に答える