PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] How to create an instance of java.io.File from a byte[] without saving the file to the disk

 November 02, 2022     arrays, file, java, memory     No comments   

Issue

I must use an existing method: method.invoke(myClassLoader, myFile.toURI().toURL()); where myFile is an instance of File and I need to create it from a byte[] without saving the file on the disk, is this possible?

This is what I tried but it creates a file on the disk and writes it:

    byte[] bytes = ...;
    File tempFile = File.createTempFile("prefix", "suffix");
    FileOutputStream fos = new FileOutputStream(tempFile);
    fos.write(bytes);
    method.invoke(myClassLoader, tempFile.toURI().toURL());

Solution

The parameter of the invoked method is URL. You can use Jimfs (An in-memory file system for Java 7+) to create a file emulation in memory and get its URL (path.toUri().toURL()).



Answered By - BccHnw
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing