uploading/streaming a file to a java servlet

Kent Johnson kent37 at tds.net
Tue Apr 19 08:05:23 EDT 2005


Vasil Slavov wrote:
> I am working on a school project written in Python (using mod_python)
> and I need to upload a file to a java servlet. Here are the high-level
> instructions given by the servlet authors:
> 
> - Open the file for reading.
> - Open an HTTP connection to the servlet and get the RequestStream object.
> - Read bytes from the file and write them to the stream until the
> entire file has been read.
> - Close the stream.

Is the request supposed to be a GET or POST or ?? I'll assume POST because sending a data stream on 
a GET is just too ugly. Even so IMO this protocol is perverse and you will have to work around it. 
You have to avoid putting the request parameters in the request body (which is normal for a POST) 
and avoid making the data into another request parameter (which would also be normal).

> Here is how the url looks like:
> http://10.0.0.21/MillenniumMobile/servlet/com.cerner.capstone.dictation.FileStorageServlet?TransactionName=AddDictationFile&FileName=myfile.wav&Username=team1&Password=password&Domain=mobj
> 
> I am having a hard time figuring out how to translate the above
> instructions into something which can be implemented in Python. How am
> I supposed to "stream" the file.

I would try something like this:
url = 
'http://10.0.0.21/MillenniumMobile/servlet/com.cerner.capstone.dictation.FileStorageServlet?TransactionName=AddDictationFile&FileName=myfile.wav&Username=team1&Password=password&Domain=mobj'
f = open(datafile, 'rb')
data = f.read()  # or whatever you need to do to get the actual data into a string
f.close()
req = urllib2.url_open(url, data)
result = req.read()

> I get a successful XML response with the following code

Your description of the protocol doesn't say anything about XML, did you leave something out?

Kent



More information about the Python-list mailing list