0
    String base64Code = dataInputStream.readUTF();

    byte[] decodedString = null;

    decodedString = Base64.decodeBase64(base64Code);


    FileOutputStream imageOutFile = new FileOutputStream(
    "E:/water-drop-after-convert.jpg");
    imageOutFile.write(decodedString);

    imageOutFile.close(); 

The problem is the data is transferred completely and if the data is in text format it is displayed correctly however when i am trying to decode image and write it on output file,it doesnt simply show up in photo viewer.

Any help would be highly appreciated

Wajih Ahmed
  • 99
  • 1
  • 2
  • 7
  • 1
    Rather than showing us the code that works, why not show the code that *doesn't* work (the image conversion code)? Then maybe we could help. – T.J. Crowder Dec 09 '12 at 07:42
  • @T.J.Crowder the image writing code is written above... it writes the image a jpg in e drive but when i try to open the image there is no image to be displayed in image viewer however the file size is equal to the original file transported. – Wajih Ahmed Dec 09 '12 at 07:45
  • Okay, from your question it sounded like you were doing something else. The code above looks fine for converting a Base-64 string and writing it to a file. So you'll have to walk through and see where things are going wrong. The most obvious thing is a problem with the Base-64 string you're receiving. – T.J. Crowder Dec 09 '12 at 07:52
  • the base 64 string is perfectly fine because when a simple text is sent it decodes and displays the text,but it is unable to write decoded byte as an image file onto drive E – Wajih Ahmed Dec 09 '12 at 07:55
  • 1
    Wajih, look at the code. There are very few possible issues. Either the string is wrong (just because it has text in it, that doesn't mean the text is right), or the `Base64.decodeBase64` function is wrong, or `FileOutputStream#write` is wrong. You'll have to walk through the code in a debugger and see which it is (I can tell you it's ***really unlikely*** to be that last one). – T.J. Crowder Dec 09 '12 at 07:58
  • If you are using windows file path will be like "E:\\water-drop-after-convert.jpg"); – Amir Qayyum Khan Dec 09 '12 at 08:02

2 Answers2

0

Once I had to convert the Image into base 64 and sent that image as stream (encoding and decoding stuff here is the code)

To Convert a file into base64 :

String filePath = "E:\\water-drop-after-convert.jpg";
File bMap =  new File(filePath);
byte[] bFile = new byte[(int) bMap.length()];
        FileInputStream fileInputStream = null;
        String imageFileBase64 = null;

        try {
            fileInputStream = new FileInputStream(bMap);
            fileInputStream.read(bFile);
            fileInputStream.close();
            imageFileBase64 = Base64.encode(bFile);   
        }catch(Exception e){
            e.printStackTrace();
        }

then on service side I did thing like that to convert image in base 64 String back into file, so that i can display. I had used this library on server side import sun.misc.BASE64Decoder;

//filePath is where you wana save image
                String filePath = "E:\\water-drop-after-convert.jpg";
                File imageFile = new File(filePath);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(imageFile);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] decodedBytes = null;
            try {
                decodedBytes = decoder.decodeBuffer(imageFileBase64);//taking input string i.e the image contents in base 64
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                fos.write(decodedBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
Amir Qayyum Khan
  • 473
  • 2
  • 15
0

DataInputStream.readUTF may be the issue. This method assumes that the text was written to the file by DataOutputStream.writeUTF. If it is not so, and you are going to read a regular text, choose a different class, like BufferedReader or Scanner. Or Java 1.7's Files.readAllBytes.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275