-1

enter image description here

Consider this file type as sharath.zip. Suppose I have encoded sharath.zip to Base64 String. When I give base64String my friend. How my friend should get sharath.zip as file name from base64String.

base64 encoded String is "5K2Q0IMKAADrq7bllYsAAAAAAAAIAOaNgealtOa5r+aMruStkMiBHwoAAOurtuWViwAAAAAAAAgkAAAAIAAAAOaNgealtOa5r+aMrgogAAABGOSRgeajgO22mseY5JGB5qOA7baax5jkkYHmo4DttprHmOStkNiFAAABAVoAJgAA"

sharath
  • 3
  • 5
  • 3
    base64 decode it? Or do you give him just the content? In that case the only way is by telling him – zapl Oct 11 '22 at 19:11
  • Yes I Just give him base64 string thats it. – sharath Oct 11 '22 at 19:12
  • When I try to decode I am getting contents inside zip file. But I dont want content inside Zip file . I just want sharath.zip as name – sharath Oct 11 '22 at 19:13
  • It's a bit unclear what you're asking. `sharath.zip` encodes to `c2hhcmF0aC56aXA=` (non-url safe encoding). That decodes back to the file name. What are you actually trying to do? – stdunbar Oct 11 '22 at 19:13
  • 1
    the file content does not contain the file name, but you can encode both the file name and it's content into a string, e.g. `"sharath.zip|c2VjcmV0IGNvbnRlbnQgOkQ="`, then when decoding, split the string on `|`, then write a file using the first part as name, the second as content after base64 decoding it. – zapl Oct 11 '22 at 19:16
  • I am not giving sharath.zip as String sharath.zip is a file i have encoded sharath.zip file and given it to my friend – sharath Oct 11 '22 at 19:16
  • @sharath: That means you've encoded the wrong thing. If you want your friend to decode the string `"sharath.zip"`, then you need to encode that, and you haven't. If you want your friend to decode the contents of the zip file, then you need to do that instead. If you give your friend the wrong thing, then they won't be able to decode something that you didn't give them. – Louis Wasserman Oct 11 '22 at 19:47

1 Answers1

0

I think for decoding, you can try something like this,

 String delims="[,]";
    String[] parts = base64ImageString.split(delims);
    String imageString = parts[1];
    byte[] imageByteArray = Base64.decode(imageString );

    InputStream is = new ByteArrayInputStream(imageByteArray);

    //Find out image type
    String mimeType = null;
    String fileExtension = null;
    try {
        mimeType = URLConnection.guessContentTypeFromStream(is); //mimeType is something like "image/jpeg"
        String delimiter="[/]";
        String[] tokens = mimeType.split(delimiter);
        fileExtension = tokens[1];
    } catch (IOException ioException){

    }
Shyam Patel
  • 381
  • 2
  • 13
  • tried this But getting mimeType as null value – sharath Oct 11 '22 at 19:15
  • Can you just send extension type as request parameter, I think it would be an easy way to achieve our goal – Shyam Patel Oct 11 '22 at 19:18
  • this thread might help? https://stackoverflow.com/questions/37269838/restore-filename-and-extension-from-base64-encoding – Shyam Patel Oct 11 '22 at 19:19
  • but base64 encoded value in like this "5K2Q0IMKAADrq7bllYsAAAAAAAAIAOaNgealtOa5r+aMruStkMiBHwoAAOurtuWViwAAAAAAAAgkAAAAIAAAAOaNgealtOa5r+aMrgogAAABGOSRgeajgO22mseY5JGB5qOA7baax5jkkYHmo4DttprHmOStkNiFAAABAVoAJgAA" – sharath Oct 11 '22 at 19:26
  • see in a request such like post, you can send like base64: "YOUR_STRING" extension: ".your_extension" at controller you can access it both. – Shyam Patel Oct 11 '22 at 19:35
  • I know the extension but I want my friend to retrieve from base64 string itself. – sharath Oct 11 '22 at 19:38
  • *but base64 encoded value in like this...* That's not a rar archive according to unrar – g00se Oct 11 '22 at 22:52