Shashank_Itmaster
Shashank_Itmaster

Reputation: 2475

Sending only 1mb of files from folder through web service

My question is that I want to send pdf files through web service with condition that only 1mb of files are taken from that folder containing many files.

Please help me to resolve this question.I am new to web service. Ask me again if it not clear. Thanks In Advance.

Upvotes: 1

Views: 766

Answers (3)

dsr
dsr

Reputation: 1316

The following method will return a list of all the files whose total size is <= 1Mb

    public List<File> getFilesList(){
    File dirLoc = new File("C:\\Temp");
    List<File> validFilesList = new ArrayList<File>();
    File[] fileList;
    final int fileSizeLimit = 1024000; // Bytes
    try {
        // select all the files whose size <= 1Mb
        fileList = dirLoc.listFiles(new FilenameFilter() {
            public boolean accept(final File dirLoc, final String fileName) {
                return (new File(dirLoc + "\\" + fileName).length() <= fileSizeLimit);
            }
        });
        long sizeCtr = fileSizeLimit;
        for(File file : fileList){
            if(file.length() <= sizeCtr){
                validFilesList.add(file);
                sizeCtr = sizeCtr - file.length();
                if(sizeCtr <= 0){
                    break;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        validFilesList = new ArrayList<File>();
    } finally {
        fileList = null;
    }
    return validFilesList;
}

Upvotes: 1

Swift-Tuttle
Swift-Tuttle

Reputation: 485

Well, I dont know if I have understood your requirements correctly and if this would help your problem but you can try this java solution for filtering the files from a directory.
You will get a list of files and then you can use the web-service specific code to send these files

File dirLoc = new File("C:\\California");
File[] fileList;
final int fileSize = 1024000;

try {
   fileList = dirLoc.listFiles(new FilenameFilter() {
       public boolean accept(final File dirLoc, final String fileName) {
           return (new File(dirLoc+"\\"+fileName).length() > fileSize);
        }
       });
} catch (Exception e) {
   e.printStackTrace();
} finally {
   fileList = null;
}

This should work.
If you just require filenames, replace the File[] with String[] and .listFiles() with list()
I cannot say much about the performance though. For a small list of files it should work pretty fast.

Upvotes: 1

dsr
dsr

Reputation: 1316

I am not sure if this is what you want but you can pick the files and check their size by :

java.io.File file = new java.io.File("myfile.txt");
file.length();

File.length()Javadoc

Send files whose size is 1 Mb.

Upvotes: 0

Related Questions