1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package UserPackage;
import java.io.File;
public class SendList implements Runnable {
private UserCommand userCmd;
// path of the dir in the server
private String path;
// number of files received
private int nbFileSent;
// index of media files type 0: image, 1:video, 2: audio
private int mediaIndex;
/**
* constructor of RecList to instantiate the class RecList for receiving a
* list of file
*
* @param path
* @param listName
* the list name of the files to receive
*/
public SendList(UserCommand userCmd, String path) {
this.path = path;
this.userCmd = userCmd;
}
/**
* to switch which media type file to send
* @param index
*/
public void setMediaIndex(int index){
mediaIndex = index;
}
/**
* return the number of files existing in the directory given by the path
* path
*
* @param path
* the path containing the files to send
* @return return the number of file in path
*/
public void sendList(String path) {
File dir = new File(path);
if (!dir.isDirectory())
return;
File[] list = dir.listFiles();
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < UserCommand.EXT[mediaIndex].length; j++){
if (list[i].getName().contains(UserCommand.EXT[mediaIndex][j])) {
String fileName = list[i].getName()
.replace(" ", "_");
try {
if (userCmd.sendFile(list[i].getAbsolutePath(), fileName) == 1) {
nbFileSent++;
}
} catch (Exception e) {
System.err.println("Failed to send: " + fileName);
e.printStackTrace();
}
}
}
}
}
/**
* return the number of file received
* from server
* @return
*/
public int getNbFileSent(){
return nbFileSent;
}
@Override
public void run() {
// TODO Auto-generated method stub
sendList(path);
}
} |