I have an html-form with a field that will take a url pointing to a (possibly binary) file (e.g. an image). The url can point to a local resource that the server can’t reach, so it needs to be fetched by the client. (This fetching is another point, my question is about writing the servlet.)
I want to upload the result of this fetch to a Java servlet (through a http POST request) and put it into an array of bytes. Since this is a low-level representation, I’m not sure if I should use Apache’s FileUpload.
Another point that confused me, is that FileItems in FileUpload only provide an OutputStream where I was expecting an InputStream to read the file.
I’m stuck after studying FileUpload. I may be that my basic knowledge of web-app development falls short.
How should I build this functionality? Please note that I’m new to this corner of Java development (although my general (i.e. SE) Java skills are very good).
Just keep using FileUpload, don’t reinvent it yourself. This will be a waste of time.
I’m not sure why you think that
FileItemonly provides anOutputStream, there’s also angetInputStream()method. Just write it to aByteArrayOutputStream.You only need to keep in mind that this may explode your server’s memory usage if there are a lot of users uploading files at the same time. Each byte of a byte array eats one byte of JVM’s memory. So imagine you’ve 100 simultaneous users who upload each a 10MB file, then 1GB of server memory is wasted to this.