• Post Reply Bookmark Topic Watch Topic
  • New Topic

URLConnection wiht FTP  RSS feed

 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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
 
sankarsv
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!