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.

Answers


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;
}

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.


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.


Need Your Help

post page with youtube video on facebook which plays directly in facebook

facebook facebook-graph-api video youtube open-graph-protocol

Here is my problem, which I haven't been able to fully solve after much searching and experimenting. We have many pages on our site which primarily hold a single YouTube video. I would like that wh...

Looking for continuous data patterns, concepts, and pitfalls

design-patterns database-design

First I apologize if this is the incorrect place to ask this kind of question. In that case I'd like to change my question to "Where I should ask this type of question."

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.