0

I want to my application Upload only .jpg and not the others files with different extensions.

Can you tell me please what i should add to my code ?

This my code :

    public void jetzt() {

      FTPClient ftpClient = new FTPClient();
      try {
          ftpClient.connect(" FTP SERVER IP");

          ftpClient.setSoTimeout(10000);
          ftpClient.enterLocalPassiveMode();
          if (ftpClient.login(" LOGIN ", " PASSWORD ")) {
              ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
              ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

              ftpClient.changeWorkingDirectory("/htdocs");

              final String serial = getManufacturerSerialNumber();
              ftpClient.makeDirectory(serial);
              ftpClient.changeWorkingDirectory(serial);


              final File folder = new File("mnt/sdcard/YmmyCandy");

              for (final File fileEntry : folder.listFiles()) {
                  try {
                      FileInputStream fs = new FileInputStream(fileEntry);
                      if (!fileEntry.isDirectory()) {
                          String fileName = fileEntry.getName();
                          ftpClient.storeFile(fileName, fs);
                          fs.close();
                          Log.i(TAG, "sent");
                      }

                  } catch (Exception e) {
                      Log.i(TAG, "error uploading");
                  }
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
J.mayer
  • 73
  • 1
  • 8

2 Answers2

1

Well, as you have name of the file, you could check if it ends with .jpg (or .jpeg!):

    String filenameArray[] = fileEntry.getName().split("\\.");
    String extension = filenameArray[filenameArray.length-1];
    if (extension.equals("jpg") || extensions.equals("jpeg")) {...}
JoKr
  • 4,976
  • 8
  • 27
  • 39
1

Here:

for (final File fileEntry : folder.listFiles()) {
    ...
    String fileName = fileEntry.getName();
    ftpClient.storeFile(fileName, fs);

You already have the file name "at hand"; but instead of writing a simple check like if (fileName.endsWith(".jpeg")) you just upload all files!

Of course, things needs some refinement. If you exactly know that all files end in .jpeg then that works, but maybe jpg, JPG and so on should work too.

And if you want a really "complete" solution, you would have to check the MIME type of each file.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks for your answer GhostCat – J.mayer Jan 10 '17 at 12:30
  • You are welcome: But just for the record: the answer you accepted is **overly** complex. There is no point in spliting the file name; you can just go and check the ending of that one string! And btw: you probably want to put that whole check into its **own** method, like `boolean isJpeg(String filneName)` - as that will make it easier for you to change the exact kind of check you are doing. – GhostCat Jan 10 '17 at 12:42