• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Uploding file to a servlet class using desktop application

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Abed Rabie
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic