Writing a filename to a OutputStream when uploading to php server so the file will be named properly
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 uplo开发者_StackOverflow社区aded, 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...
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.
精彩评论