URL Connection (FTP) in Java - Simple Question

后端 未结 3 1746
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 02:17

I have a simple question. I\'m trying to upload a file to my ftp server in Java.

I have a file on my computer, and I want to make a copy of that file and upload it. I t

3条回答
  •  星月不相逢
    2021-01-22 02:46

    Use a BufferedInputStream to read and BufferedOutputStream to write. Take a look at this post: http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/

    InputStream is = new FileInputStream(localfilename);
    BufferedInputStream bis = new BufferedInputStream(is);
    OutputStream os =m_client.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);
    byte[] buffer = new byte[1024];
    int readCount;
    while( (readCount = bis.read(buffer)) > 0) {
        bos.write(buffer, 0, readCount);
    }
    bos.close();
    

提交回复
热议问题