Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

java - Convert ByteArrayInputStream to File without saving to Disk

This question concerns file handling using Java/Groovy/Grails. I am still new to these technologies, so bear with me.

I am uploading a file to Stripe after receiving the file from a user submitted POST request from the client side. I don't want to save it to the disk if it can be avoided.

I receive the file and parse into an array of bytes.

List<MultipartFile> files = ((MultipartHttpServletRequest) request).getMultiFileMap().collect {
                it.value
            }.flatten()
            try {
                if (files.size > 0) {
                    // parse file and get byte array input stream
                    MultipartFile file = files.first()
                    String fileName = file.originalFilename
                    byte[] bytes = file.inputStream.getBytes()
                    callStripeFileService(bytes)
            }

This byte array is passed to a service which makes the call to Stripe as indicated in their docs.

    callStripeFileService(byte[] bytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes)
        String tempFilePath = 
             "src/main/resources/tmp/${fileName}"
        IOUtils.copy(bais, new FileOutputStream(tempFilePath))

        FileCreateParams params = FileCreateParams.builder()
            .setFile(new java.io.File(tempFilePath))
            .setPurpose(FileCreateParams.Purpose.RECEIPT)      
            .build()

        File upload = File.create(params)
    }

This is working just fine, but I dislike writing the file to disk only to have to remove it eventually. I also suspect that there must be performance implications to writing to disk.

I would love to just pass a stream to Stripe, but the .setFile() method only accepts a File or FileInputStream.

Is there any way to create a valid Java File object without saving anything to disk?

question from:https://stackoverflow.com/questions/65621408/convert-bytearrayinputstream-to-file-without-saving-to-disk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No; a java.io.File is a File, hence the name. No such thing as a virtual file-like thing capturable as a File object - java.io.File is a very thin wrapper, it has one field, and that is the path, that's all it is. All methods on it, and things like new FileInputStream(...) just read the path from that File object and do all the work itself, hence making it impossible. Well designed java APIs do not use File; they use, for example, InputStream. If the stripe API only allows File, then it's badly designed API. The fix would be to send a pull request to the repo with a fix for it.

Presumably, the authors messed up; that variant that takes a FileInputStream is an error, and it should have taken an InputStream instead. I'll go on record with it, why not: A method whose parameter type is FileInputStream is broken. Not broken in the sense that 'tests can catch it', but as broken as a method that adds 2 numbers together named 'subtract'.

TL;DR: Not possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...