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

4

2 回答 2

0

DataInputStream.readUTF 可能是问题所在。此方法假定文本是由 DataOutputStream.writeUTF 写入文件的。如果不是这样,并且您要阅读常规文本,请选择不同的类,例如 BufferedReader 或 Scanner。或 Java 1.7 的 Files.readAllBytes。

于 2012-12-09T08:06:00.550 回答
0

一旦我不得不将图像转换为base 64并将该图像作为流发送(这里的编码和解码内容是代码)

要将文件转换为 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();
        }

然后在服务端我做了类似的事情将base 64 String中的图像转换回文件,以便我可以显示。我在服务器端使用过这个库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();
            }
于 2012-12-09T07:51:17.507 回答