473,614 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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/MillenniumMobil e/servlet/"
servlet = "com.cerner.cap stone.dictation .FileStorageSer vlet"
params = urllib.urlencod e({'Transaction Name':'AddDicta tionFile','File Name':'myfakefi le.wav'})
request = urllib2.Request ("".join([url, servlet]), params)
request.add_hea der('User-Agent', 'Velositer')
request.add_hea der('Cookie', sessid)
opener = urllib2.build_o pener()
datastream = opener.open(req uest)
# I am using mod_python
req.write(datas tream.read())

I tried urllib2_file.py :
http://fabien.seisen.o rg/python/urllib2_multipa rt.html

with which you can do stuff like:
data = [('TransactionNa me','AddDictati onFile'),('File Name','back9.jp g'),('file',ope n(file,'rb'))]
request = urllib2.Request ("".join([url, servlet]), data, headers)
response = urllib2.urlopen (request).read( )

and this:
http://aspn.activestat e.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 4254
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/MillenniumMobil e/servlet/com.cerner.caps tone.dictation. FileStorageServ let?Transaction Name=AddDictati onFile&FileName =myfile.wav&Use rname=team1&Pas sword=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_ope n(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
3665
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 , /webapps/sue) and i have added the folders bob, sue in my server.xml(in the context path). When i am trying to run jsp files from my browser, it works fine. But, the following jsp files reports some exceptions which is quite hard to understand. Here is...
2
22169
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> <td><input name="sort" type="submit" value="Description"></td>
1
2148
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 errors: org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: -1 in the jsp file: null Generated servlet error: Compiling 1 source file
7
18798
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); String fileName = filePath.substring(filePath.lastIndexOf("/")+1); System.out.println("File Name:\t"+fileName); try { File file = new File(filePath);
1
2724
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 XMLReaderAdapter().setLocale((Locale)request.getAttribute("locale")) %>" TYPE="text/css"> but i get a exception
2
5011
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 com.oreilly.servlet.MultipartRequest;
3
2039
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.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoadImage extends HttpServlet{
1
2845
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 silverlight client to Silverlight streaming service. I tried to user WebClient and HttpWebRequest for that purpose but, unfortunately I can found the way to do so. There are some problems with both classes. 1. There is no property of get...
3
3081
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 confused. Is that true that file uploaded image can not show on servlet.Its store to database or memory..?? Please assist me. Thanks.
0
8197
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8287
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8443
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.