I'm trying to create a file and send it to the server. My errors: "No such file of directory" - although I have already saved the photo and "how can I properly send this file"

new Thread(new Runnable(){ public void run(){ FTPClient ftp = null; try { ftp = new FTPClient(); ftp.connect("ftp.example.com", 21); ftp.login("user", "password"); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); directory = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS),"MyDoc"); OutputStream outputStream = null; boolean success = false; try { File file_txt = new File(directory,"text.txt"); BufferedWriter in = new BufferedWriter(new FileWriter(file_txt)); in.write("Text"); in.close(); //boolean result = ftp.storeFile("text.txt", in); in.close(); } finally { if (outputStream != null) { outputStream.close(); } } } catch (IOException e) { e.printStackTrace(); } finally { } } }).start(); 

Help !!!

  • In my opinion, you need to stop copying the code and start to figure out what each line does. - Athari

1 answer 1

You are trying to create a file in a folder that may not exist ( "MyDoc" ). You first need to make sure that it exists, or create it. For this after

 directory = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS),"MyDoc"); 

add

 directory.mkdirs(); 
  • as I understand it, the buffer is not closed and with the help of storeFile it can be loaded onto the serv, but something is wrong there ...... - Dmitriy Dev
  • @DmitryKoverko, did you even read my answer? You have a mistake, I wrote why it arises, and made a decision. WHAT at all for the buffer? What's on him? - Vladyslav Matviienko
  • so the directory and file I created (thanks to you). And now I open the stream for reading and want to send to the server: fis = new FileInputStream (Sourcefile); boolean done = ftp.storeFile ("text.txt", fis); But the fact is that an empty file is created and an error occurs during the reading, and consequently, an error is sent. - Dmitriy Dev
  • @DmitryKoverko, ask a new question if you have a new problem. Do not dump everything in a pile, otherwise your question will not be of any use to others. - Vladyslav Matviienko
  • the question is closed - the problem is solved! - Dmitriy Dev