this should be real quick. Currently I have this code to write an image to an output stream and upload it to a server:

        OutputStream os = connection.getOutputStream();

        BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
        int totalBytes = fis.available();

        for(int i = 0; i < totalBytes; i++) {
            os.write(fis.read());
        }

Etc. (I know that for loop for writing the data is super basic, I'm gonna make it a bit better in the future I'm just trying to get things working at a basic level here).

Anyways, as you can see I'm writing a file to the stream and sending it out, this works just fine. My problem is, I have no way as of yet to transmit the proper file name as well. On the php end I just set it to a generic timestamp file name, and I don't want to use the default image name on the phone either, the image uploaded will include the user's name who uploaded, including a timestamp which I can add on the php end just fine. I just need to know how to include something like a namevaluepair which has the user's name in it, so on the php side the filename can include that string. Thanks!

edit: dont know why i was saying data, its just an output stream...

Comments

How is your app sending the file (via post, other standard http protocol etc?), and how is the remote PHP script reading in the file ($_FILE, sockets server, etc)? The protocols used might make all the difference.

Accepted Answer

The file name is usually sent in the headers. For POST (which is usually what you are using when you obtain an output stream), you should have a content-type of multipart/form-data and each part will have a content-disposition header. For a part corresponding to the file (the only part, in your case), the content-disposition header should have a filename="foo.txt" component. The server then saves the contents in a temp file and hands your script the info contained in the content-disposition header (along with a means of getting to the uploaded file itself).

See RFC 1867 for details.

Written by Ted Hopp
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki