473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

uploading/streaming a file to a java servlet

I apologize for the long email. I hope somebody will have time to read
it and give some suggestions.

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 (the instructions are geared
towards my C#/VB competition (and I really want to show them how cool
Python is):

- 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.

Here is how the url looks like:
http://10.0.0.21/MillenniumMobile/se...rd&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 get a successful XML response with the following code, but
obviously, it doesn't do anything useful because it doesn't actually
upload the file:

url = "http://10.0.0.21/MillenniumMobile/servlet/"
servlet = "com.cerner.capstone.dictation.FileStorageServ let"
params = urllib.urlencode({'TransactionName':'AddDictationF ile','FileName':'myfakefile.wav'})
request = urllib2.Request("".join([url, servlet]), params)
request.add_header('User-Agent', 'Velositer')
request.add_header('Cookie', sessid)
opener = urllib2.build_opener()
datastream = opener.open(request)
# I am using mod_python
req.write(datastream.read())

I tried urllib2_file.py:
http://fabien.seisen.org/python/urllib2_multipart.html

with which you can do stuff like:
data = [('TransactionName','AddDictationFile'),('FileName' ,'back9.jpg'),('file',open(file,'rb'))]
request = urllib2.Request("".join([url, servlet]), data, headers)
response = urllib2.urlopen(request).read()

and this:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

but they don't work. I get errors that the servlet can't parse the XML request.

Any suggestions would be greatly appreciated.

Thanks in advance,
Vasil
Jul 19 '05 #1
1 4217
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/se...rd&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?T ransactionName=AddDictationFile&FileName=myfile.wa v&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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob ,...
2
by: kaiwing18 | last post by:
Hi, I have a following jsp file called "showAsset.jsp" : <form action="showAsset.jsp" method="post"> <tr BGCOLOR=YELLOW> <td><input name="sort" type="submit" value="Asset_Number" > </td>...
1
by: rohitkec | last post by:
I am new to both java andb apache tomcat.i am presently using apache tomcat 4.1.31 & j2re-1_4_2_10.when i am running http://127.0.0.1:8080/admin/login.jsp or admin file i am getting the following...
7
madhoriya22
by: madhoriya22 | last post by:
Hi, I am trying to read a file sent from client to server. Here is how I am trying it .... String filePath = request.getParameter("SelectCSVFile"); System.out.println("path:::---> "+filePath);...
1
by: sathyak83 | last post by:
i want to format the numbers using css for each country ie if country is france the format=0.0.0 if country is UK=00.00 i use the below code <LINK REL=stylesheet HREF="<%= new...
2
by: praveenkumarvpk | last post by:
Hi friends Please help me! Following is my servlet-code import java.util.Enumeration; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import...
3
by: jatin299 | last post by:
hi ..problem in uploading image..using servlet to upload image in mysql..use html form so user given the path of image..but giving error.here is the code..help me on this. import java.sql.*;...
1
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from...
3
by: vijaykumardahiya | last post by:
Hello To Every One, I want to know that when I upload the File like a image from html page Its not show on servlet page using appropriate logic. I read the FileUpload Home page.But I am still...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.