Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File transfer using Java Http  RSS feed

 
Jagadeesh Kumar Sastry.K
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Am working on module for which i need to transfer a file using java Http. Can any body please let me know how to go about it. I cannot use any third party jars(though it is freeware). I have to use jdk api only. Any help will be greately appreciated.

thanks,
Jags
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which direction?
Sending a file out to a client is pretty easy, setup a servlet, set the headers, and send the file content in the appropriate encoding.

Sending a file to a server is also fairly easy, you want to post to
a servlet and read the stream as arguments.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like what Pat said, you can use URLConnection class to send an HTTP request to a server. That class is part of JDK.
 
Jagadeesh Kumar Sastry.K
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pat and Freddy,

Thanks for the reply. Actually we are using core java. We are not using servlets here and we are using HttpURLConnection itself. But no luck.

First of all is it possible to complete current requirement using Core java or we need to write Servlets for this?

Please find my requirement

1) Need to send a file to one of appliance.
2) Rather than appliance pulling file from machine, we have to push the file.
3) Appliance will accept only Http connections.

So, we are trying to push the file using HttpURLConnection. Please find the code which we are trying.

public class Demohttp
{
static URL url;

public static String encode (String source)
{
BASE64Encoder enc = new sun.misc.BASE64Encoder();
return(enc.encode(source.getBytes()));
}



public static void main(String args[])
{
try
{
int x;
url = new URL("http://1.2.2.4");
HttpURLConnection con;
BufferedInputStream bufIn;
String user="admin";
String password="admin123";

String stringUserPassword = user + ":" + password;
String base64UserPassword = encode(stringUserPassword);

con = (HttpURLConnection)url.openConnection();

con.setDoOutput(true);
con.setDoInput(true);

con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Authorization", "Basic "+base64UserPassword);

con.connect();
}
catch (Exception e)
{
System.out.println("in catch" +e);
}
}
}

The above code does the connectivity and once connection is done, we have to push file from our local location to appliance.

Please let me know ,for the current requirement, is the approach works or do we need to use anything else.

thanks,
Jags
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jagadeesh Sastry:
Hi Pat and Freddy,
First of all is it possible to complete current requirement using Core java or we need to write Servlets for this?


It is always possible to write anything in any limited set, the javax classes all started as code from java. So the answer is yes, you can.

But why would you want to?

Servlets do 99% of the work for you. You really need to have a HTTP daemon/server to do what you want, so why not use Tomcat or Resin or some other HTTP server that handles servlets?

NetBeans and Eclipse make writing servlets very easy, include bundled servers, etc. I'm sure other IDE/enviroments do as well.

Code reuse is what you want.
 
Jagadeesh Kumar Sastry.K
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pat,

Actually this code will be running at customers location. Most of the customer are reluctant to install any of the softwares. So, we are preferring Core java. If this task can be done in core java, please let me know how to proceed. This is literally a blocking bug.

thanks,
Jags
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of "appliance" is that? If it accepts HTTP only, and you're supposed to upload files to it, then it had better implement a component that can accept files.

The client side -which you need to implement- can be done using HttpUrlConnection alone. This article should get you going.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jagadeesh Sastry:
Hi Pat,

Actually this code will be running at customers location. Most of the customer are reluctant to install any of the softwares. So, we are preferring Core java. If this task can be done in core java, please let me know how to proceed. This is literally a blocking bug.

thanks,
Jags


Even if you implented the whole thing in core Java, the client would still need to install the program you've written.
What's the difference to them if you ask them to install you program or Tomcat?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jagadeesh Sastry:

BASE64Encoder enc = new sun.misc.BASE64Encoder();


You really should not use a sun.misc function in a limited environment. Its not part of "Core Java" and while Sun currently provides the classes, they explicitly tell you not to use them in general.

I guess I'm not understanding the requirements, installing the code and the jars to run it are pretty normal. Some embedded systems don't have the space, or have limitations to the JVM, but those worlds have lots of restrictions.
 
Jagadeesh Kumar Sastry.K
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

is there any file size limit in servlets to push the file to destination machine?i.e., it can only push 100 mb like that???

For file push, please find the below code.

Here for test purpose, we are using GUI with upload button etc., But in real time we don't want any gui and as well upload button also, because we know the file name and the program should automatically push the file, which is available in specified directory, every one hour.

For, pile push below is the code, which we are using. Can somebody let me know, is ther any better way to do it?

=======================



Thanks,
Jags

[ UD: added CODE tags to preserve legibility; please UseCodeTags when posting code of any length ]
[ September 11, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!