Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLConnection wiht FTP

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
sankarsv
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic