i'm quite new to this java thing, but i'm trying to let my program communicate with an ftp server. logging in and out is no problem anymore, but uploading does not seem to work; nullpointer exceptions are my doom. is there any somewhat experienced knight-on-a-white-horse caring to point the likely totally obvious mistake in my code? here is the api for the apache.commons.net.ftp.*: clicky Code: package ftp; import java.io.*; import java.util.ArrayList; import java.util.BitSet; import java.net.SocketException; import org.apache.commons.net.ftp.*; /** * @author alpaca * */ public class Connection { FTPClient ftp = new FTPClient(); OutputStream stream = null; InputStream upload = null; public boolean connectAndLogon(String server,String user,String password){ //connection with server try { ftp.connect(server); } catch (SocketException e) { System.out.println("connect SocketExeption"); e.printStackTrace(); } catch (IOException e) { System.out.println("connect IOException"); e.printStackTrace(); } if (ftp.isConnected()) { System.out.println("verbonden met " + server); } //loggin in try { if (ftp.login(user, password)) {System.out.println("logged in as " + user);} } catch (IOException e) { System.out.println("login IOExeption"); e.printStackTrace(); } //checking if connected boolean connected = ftp.isConnected(); if (!connected) { try { ftp.disconnect(); } catch (IOException e) { // no connection is found e.printStackTrace(); } return false; } return true; } public boolean upload(BitSet document, String serverpath) { boolean gelukt = true; //opening stream to upload try { stream = ftp.storeFileStream(serverpath); } catch (IOException e) { System.out.println("upload ioexeption"); e.printStackTrace(); } //change from bitset to bytearray byte[] bytearray = new byte[document.length()/8+1]; for (int i=0; i<document.length(); i++) { if (document.get(i)) { bytearray[bytearray.length-i/8-1] |= 1<<(i%8); } } // writing in the stream (uploading to server) try { stream.write(bytearray); } catch (IOException e) { System.out.println("stream.write ioexeption"); e.printStackTrace(); } return gelukt; } public boolean disconnect() { int i = 0; // if still connected, he'll try 3 times to disconnect while (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { System.out.println("disconnect IOException"); e.printStackTrace(); } if(i == 3) { System.out.println("could not disconnect"); return false; } i++; } System.out.println("disconnected"); return true; } } thank you; alpaca
Any particular reason for the BitSet? are you uploading something you've created in ram that don't exist on disk? I would probably deal with it something like this. Code: import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.SocketException; import org.apache.commons.net.ftp.FTPClient; /** * @author alpaca * */ public class Connection { FTPClient ftp = new FTPClient(); public boolean connectAndLogon(String server, String user, String password) { //connection with server try { ftp.connect(server); } catch (SocketException e) { System.out.println("connect SocketExeption"); e.printStackTrace(); } catch (IOException e) { System.out.println("connect IOException"); e.printStackTrace(); } if (ftp.isConnected()) { System.out.println("verbonden met " + server); } //loggin in try { if (ftp.login(user, password)) { System.out.println("logged in as " + user); } } catch (IOException e) { System.out.println("login IOExeption"); e.printStackTrace(); } //checking if connected boolean connected = ftp.isConnected(); if (!connected) { try { ftp.disconnect(); } catch (IOException e) { // no connection is found e.printStackTrace(); } return false; } return true; } public boolean upload(File document, String serverpath) { BufferedOutputStream output = null; BufferedInputStream input = null; boolean success = true; /** If file don't exist fail */ success = !document.exists(); if (success) { try { /** Create remote file and get stream to it */ /** Make a Buffered output out of the ftp connection, * we'll explain why buffered later */ output = new BufferedOutputStream(ftp.storeFileStream(serverpath)); } catch (IOException e) { System.out.println("upload ioexeption"); e.printStackTrace(); success = false; output = null; } } if (success) { try { /** Open InputStream, we'll explain why buffered later */ input = new BufferedInputStream(new FileInputStream(document)); } catch (FileNotFoundException e) { System.out.println("upload ioexeption"); e.printStackTrace(); success = false; input = null; } } /** Since we have Buffered input and Buffered output * we can write a simple loop for byte by byte copy * with little performance impact. If we weren't using * buffered streams use an intermediate byte array to * copy groups at a time, the larger the chunk the better * but don't get blocked for read on your IO. */ if (success) { try { int data; while ((data = input.read()) != -1) { output.write(data); } } catch (IOException e) { /** if we get in here, the state of the remote file * is unknowen it may be partly written * it may not be, it may be cleand up by the * ftp server it may not be. Some research and * testing should be done here. Then take appropate * action to clean up after yourself where possible. */ success = false; System.out.println("upload ioexeption"); e.printStackTrace(); } } /** Writing complete. * Flush the output then close the * input and the output. */ if (output != null) { try { output.flush(); output.close(); } catch (IOException e) { success = false; System.out.println("upload ioexeption"); e.printStackTrace(); } } if (input != null) { try { input.close(); } catch (IOException e) { success = false; System.out.println("upload ioexeption"); e.printStackTrace(); } } /** return the end result */ return success; } public boolean disconnect() { int i = 0; // if still connected, he'll try 3 times to disconnect while (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { System.out.println("disconnect IOException"); e.printStackTrace(); } if (i == 3) { System.out.println("could not disconnect"); return false; } i++; } System.out.println("disconnected"); return true; } } unless you ARE uploading something you created in ram. Then i'd look into ByteArrayInputStream. Cause what you have now, is creating a COPY of the content of the BitSet.. Thats a lot of ram when your uploading large things.. as for your crash, i'm almost sure its in the bit shifting loop somewhere.. I just can't see it right now, but mod and devision in such a tight loop.. o the cpu you'll eat. I'd advise staying away from bit buffers in java unless you got a very good reason, like your in nio, and are writing a decompiler..