Forums Register Login

URLConnection wiht FTP

+Pie Number of slices to send: Send
I have this class which is using FTP as a protocol to send files on a URL connection. I get the exception :UnknownServiceException on the line siteCon.connect(). Any help appreciated.
Here is the code:
private class FtpFile {
public FtpFile( String url, String fname) throws Exception
{
URL site = new URL(url);
URLConnection siteCon = site.openConnection();
siteCon.setDoInput(true);
siteCon.setDoOutput(true);
siteCon.connect();
System.out.println("Allowed interaction is " + siteCon.getAllowUserInteraction() + siteCon.getDoOutput() );
OutputStream out = siteCon.getOutputStream();
// Send the file out
File file_in= new File(fname);
FileInputStream is= new FileInputStream(file_in);
int c;
byte[] bytes = new byte[1024];
int total_bytes=0;
while((c=is.read(bytes)) !=-1)
{
total_bytes +=c;
out.write(bytes,0,c);
}
out.flush();
out.close();
//Verify that the file is there
int read_bytes = 0;
site = new URL(url+"//"+fname);
siteCon = site.openConnection();
siteCon.setDoInput(true);
siteCon.connect();
InputStream in = siteCon.getInputStream();

while((c=in.read(bytes)) != -1)
{
read_bytes += c;
}
System.out.println("File read from destination, total bytes = " + read_bytes);


}
}

Savithri
+Pie Number of slices to send: Send
In http, call to connect() method id not required....I donno in ftp. But u can still try the same....

Originally posted by Savithri Devaraj:
I have this class which is using FTP as a protocol to send files on a URL connection. I get the exception :UnknownServiceException on the line siteCon.connect(). Any help appreciated.
Here is the code:
private class FtpFile {
public FtpFile( String url, String fname) throws Exception
{
URL site = new URL(url);
URLConnection siteCon = site.openConnection();
siteCon.setDoInput(true);
siteCon.setDoOutput(true);
siteCon.connect();
System.out.println("Allowed interaction is " + siteCon.getAllowUserInteraction() + siteCon.getDoOutput() );
OutputStream out = siteCon.getOutputStream();
// Send the file out
File file_in= new File(fname);
FileInputStream is= new FileInputStream(file_in);
int c;
byte[] bytes = new byte[1024];
int total_bytes=0;
while((c=is.read(bytes)) !=-1)
{
total_bytes +=c;
out.write(bytes,0,c);
}
out.flush();
out.close();
//Verify that the file is there
int read_bytes = 0;
site = new URL(url+"//"+fname);
siteCon = site.openConnection();
siteCon.setDoInput(true);
siteCon.connect();
InputStream in = siteCon.getInputStream();

while((c=in.read(bytes)) != -1)
{
read_bytes += c;
}
System.out.println("File read from destination, total bytes = " + read_bytes);


}
}

Savithri


+Pie Number of slices to send: Send
 

Originally posted by sankarsv:
In http, call to connect() method id not required....I donno in ftp. But u can still try the same....


Sorry, the exception is on the getOutputStream() method , not on the connect(). I tried without the connect(), the same exception is still thrown
Savithri
+Pie Number of slices to send: Send
Hi,
I donno much about ftp. What i feel is, u dont have permission to write into ftp site. Try getInputStream. If it works means, it shows u that it is read-only url. You cant write into it. Get the write-access and try. Lemme know if it is useful......
Sankar

Originally posted by Savithri Devaraj:
Sorry, the exception is on the getOutputStream() method , not on the connect(). I tried without the connect(), the same exception is still thrown
Savithri


We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1847 times.
Similar Threads
Servlet sends zip file to client
How to run FTP.exe with/without IP Address, Username and password?
send files over the socket
Help Please- on design approach for data collection across n/w
Writing FTP Client in Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:59:42.