1

If I'm using Java and I have a String of a Data URI, is it possible to create a File from that String value?

For example, here's what I'm currently trying to do:

//A data URI representing a txt file 
String dataUri = "data:text/plain;base64,VGVzdGluZy4=";

//This line works fine. The URI is created
URI testURI = new URI(dataUri);

//This line throws this exception: java.lang.IllegalArgumentException: URI is not hierarchical
File test = new File(testURI);

I've looked at posts related to the exception (Why is my URI not hierarchical?, and Java Jar file: use resource errors: URI is not hierarchical), but neither of these original questions are trying to do what I'm trying to do. Also, I understand that the issue is because the URI is not in this form C:\path\to\a\file.example (hierarchical). This is clearly stated in the documentation. So I understand that this is not a proper way to go about what I'm trying to do, I'm just not sure what else to try.

Is it possible to convert a data URI to a File (or perhaps a similar class) in Java?

Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
  • This does seem to be a possible duplicate of that question in the sense that the accepted answer for that question is a general conversion of a base64 encoded file to a `byte[]`. However, you have to admit that the post is not very well named for that solution, which is arguably why I didn't find it myself. – Francis Bartkowiak May 03 '18 at 14:08
  • 1
    Agreed. You should mark your question as duplicate now. :) – 31piy May 03 '18 at 14:10
  • Ya know, I wasn't aware that I could do that? I learn something new here everyday. :) Thanks for pointing me in the right direction. – Francis Bartkowiak May 03 '18 at 14:13

0 Answers0