aspose file tools*
The moose likes Servlets and the fly likes Uploding file to a servlet class using desktop application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploding file to a servlet class using desktop application" Watch "Uploding file to a servlet class using desktop application" New topic
Author

Uploding file to a servlet class using desktop application

Abed Rabie
Ranch Hand

Joined: Jun 24, 2003
Posts: 74
Dear All,

Did anyone able to upload a flat file (myfile.txt) to a servlet class from within a java desktop application??

If anyone know how ?? please inform me .

Thanks

Aabed


Abed Rabie
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interesting question. I'd get an HTTP sniffer (I use HTTPLook), run a browser upload link and see how the data looks going out. Read up on the W3C header standards and try to imitate the browser with a socket or HttpUrlConnection or whatever. If you've already written the server to accept file uploads, you know better than anyone else what kind of data stream you expect from the client. Just make the client make the server happy!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abed Rabie
Ranch Hand

Joined: Jun 24, 2003
Posts: 74
I'm not planning to use http protocol or html files. All i need is to use the java.net package capability to connect my desktop application to a web server servlet class. Is it possible or not??

The uploaded file will be a flat file (.txt or .csv file)

Thanks for help
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
It is most certainly possible!! I hope that answers your question

if you are not using http prototcol, then you will run into problems getting across corporate firewalls, unless you are running your server on port 80
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you'll have a very hard time calling a servlet without HTTP protocol. If you don't send the proper HTTP headers the web server won't know what to do with your request. You can implement the HTTP client protocol with raw sockets but it's not that much fun. Take a look at HttpUrlConnection and see how many methods you might have to implement.
Abed Rabie
Ranch Hand

Joined: Jun 24, 2003
Posts: 74
Dear sir,

Did u have any link to a sample code that handle this problem using Http protocol.

Thanks alot for help and best regards

Aabed
Anthony Bull
Greenhorn

Joined: Nov 08, 2004
Posts: 8
I am using two Jakarta Commons libraries to do this very thing, except I am uploading binary files.

Use the HttpClient library to send HttpServletRequests containing files or whatever, and on your servlet use the FileUpload library to grab the results and save the file to disk.
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
I'm working on some stuff where I have an applet that need to communicate with a database it's not allowed to acces because of the sandbox it run in. I use a servlet on the machine the applet was served from as kindof a proxy to do all the database work. I do about exactly what you're trying to do.
The applet (client) sends text commands (sql queries) to the servlet like this

try {
// Construct a URL referring to the servlet
java.net.URL url = new java.net.URL( "http://localhost:8084/myApp/myServlet");
java.net.HttpURLConnection connection = (java.net.HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("ContentType", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
// Send a POST message to the servlet
String sql = "sql=" + java.net.URLEncoder.encode(str, "UTF-8");
sql = sql + "&command=select";
byte bytes[] = sql.getBytes();
//at this point "sql" is the text I want to send
connection.setRequestProperty("Content-length", "" + bytes.length);
connection.connect();
OutputStream out = connection.getOutputStream();
out.write(bytes);
out.flush();
//in my case, the servlet return a binary object (a Vector)
//you may want to just return text or nothing
// Get the response as an ObjectInputStream
InputStream in = connection.getInputStream();
ObjectInputStream result = new ObjectInputStream(in);

// Read the Date object from the stream
Object obj = result.readObject();
v = new myRowSet((java.util.Vector)obj);

// Return the Vector containing the data
return v;
}
catch (Exception e) {
//javax.swing.JOptionPane.showMessageDialog(m_App, "An Error Occured Connecting To The Database!", "SQL Error", javax.swing.JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
return null;
}
Abed Rabie
Ranch Hand

Joined: Jun 24, 2003
Posts: 74
Dear Dave Robbins ,

Thanks alot for your reply but I'd like to ask if I use a desktop application other than Applet is it possible to use this code or not.

Also I want you to send me a sample class for the servlet class.

Thanks again for your help

Best regards

Aabed
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
it should be even easier from an application
In an applet there are tight restrictions on what other machines you can talk to (you can only talk to the machine the applet was served from) That's why I'm doing this, the database might be on a different machine, which the applet couldn't talk to. So in the application do something like I posted and make the url you send your data with be like "http://www.your_machine.com:8080/yourServlet?data=blahblahblah"
and in the servlet do something like this
String strData = request.getParameter("data");
strData will contain blahblahblah
you just sending you text file as POST data
I can't remember what kind of data you said your sending, you'll probably want to encode it, look at this page for a nice trick to do it

http://www.rgagnon.com/javadetails/java-0084.html

how are you doing this?
I find Netbeans to be an excellent environment for servlet work, it lets you debug the code and single step thru and see exactly what's going on

good luck
Dave
Abed Rabie
Ranch Hand

Joined: Jun 24, 2003
Posts: 74
Thanks alot Dave for this great link. I think It'll help me alot.
Keep in touch my dear friend.

Thanks again and best regards

Aabed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploding file to a servlet class using desktop application