Results 1 to 4 of 4
  1. #1
    cece0808 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Question URLConnection problem

    Hi,

    I'm trying to send file over ftp connection to my ftp server. I'm using tutorial from Upload files to FTP server using URLConnection class and it's working fine as long my username doesn't contain '@'. Problem is, that my username for the wanted ftp-server is username@nameoftheftpserver. String ftpUrl = "ftp://%s:%s@%s/%s;type=i" already has @-mark and I believe this is causing the crashing. I tried to connect with exactly same code to another ftp server, with username that doesn't contain @-mark and it worked perfectly. Does anyone know how to get this thing working with username that has @... Below is the code from the tutorial that explains the mechanic of code. Thank you!


    package net.codejava.ftp;

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;

    public class FtpUrlUpload {

    private static final int BUFFER_SIZE = 4096;

    public static void main(String[] args) {
    String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
    String host = "www.myserver.com";
    String user = "tom";
    String pass = "secret";
    String filePath = "E:/Work/Project.zip";
    String uploadPath = "/MyProjects/archive/Project.zip";

    ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);
    System.out.println("Upload URL: " + ftpUrl);

    try {
    URL url = new URL(ftpUrl);
    URLConnection conn = url.openConnection();
    OutputStream outputStream = conn.getOutputStream();
    FileInputStream inputStream = new FileInputStream(filePath);

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
    }

    inputStream.close();
    outputStream.close();

    System.out.println("File uploaded");
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: URLConnection problem

    Substitute the '@' in your username for '%40' (the hex value for '@').
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: URLConnection problem

    In fact, rather than manually doing that use the URLEncoder.encode() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    cece0808 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Re: URLConnection problem

    Quote Originally Posted by Tolls View Post
    In fact, rather than manually doing that use the URLEncoder.encode() method.
    Thank you very much! That did the trick :)

Similar Threads

  1. URLConnection
    By warrier in forum Networking
    Replies: 4
    Last Post: 02-18-2013, 05:22 PM
  2. URLConnection
    By tnrh1 in forum New To Java
    Replies: 9
    Last Post: 12-20-2011, 02:03 PM
  3. UrlConnection question(s)
    By dhazwa in forum New To Java
    Replies: 1
    Last Post: 08-31-2011, 09:56 AM
  4. URLConnection Efficiency
    By Lil_Aziz1 in forum New To Java
    Replies: 22
    Last Post: 08-19-2010, 06:27 PM
  5. Opening URLConnection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-24-2007, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •