1

Im try to upload all txt files from download folder in sdcard I use file filter with ("txt") But the app doesnt upload any txt file I try the app without the file filter and upload successful one txt file but i want to upload all files What is the wrong in my code... Why the app doesn't upload filtered files?? How to get filtered file to upload?

This my MainActivity.class

public class MainActivity extends Activity 
{
@Override
protected void onCreate(Bundle      savedInstanceState)
{
        super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    uploadFile(sourceFileUri);}

public static   String sourceFileUri ="/mnt/sdcard/downloads";




public static void main(String[] args){


    File dir =new File(sourceFileUri);
    FilenameFilter filter =new MyFileFilter();
    File[] files = dir.listFiles(filter);
    for(File f : files)System.out.println(f.getName());   

}






public void uploadFile(String sourceFileUri) {

    new UploadFileAsync().execute("");}



private class UploadFileAsync extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        try {

            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            File sourceFile = new File(sourceFileUri);

            if (sourceFile.isFile()) {

                try {
                    String upLoadServerUri = "http://sssssttttt.esy.es/uploaded.php?";

                    // open a URL connection to the Servlet
                    FileInputStream fileInputStream = new FileInputStream(
                        sourceFile);
                    URL url = new URL(upLoadServerUri);

                    // Open a HTTP connection to the URL
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true); // Allow Inputs
                    conn.setDoOutput(true); // Allow Outputs
                    conn.setUseCaches(false); // Don't use a Cached Copy
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE",
                                            "multipart/form-data");
                    conn.setRequestProperty("Content-Type",
                                            "multipart/form-data;boundary=" + boundary);
                    conn.setRequestProperty("bill", sourceFileUri);

                    dos = new DataOutputStream(conn.getOutputStream());

                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                                   + sourceFileUri + "\"" + lineEnd);

                    dos.writeBytes(lineEnd);

                    // create a buffer of maximum size
                    bytesAvailable = fileInputStream.available();

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];

                    // read file and write it into form...
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                    while (bytesRead > 0) {

                        dos.write(buffer, 0, bufferSize);
                        bytesAvailable = fileInputStream.available();
                        bufferSize = Math
                            .min(bytesAvailable, maxBufferSize);
                        bytesRead = fileInputStream.read(buffer, 0,
                                                         bufferSize);

                    }

                    // send multipart form data necesssary after file
                    // data...
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens
                                   + lineEnd);

                    // Responses from the server (code and message)
                int serverResponseCode = conn.getResponseCode();
                    String serverResponseMessage = conn
                        .getResponseMessage();

                    if (serverResponseCode == 200) {

                        // messageText.setText(msg);
                        //Toast.makeText(ctx, "File Upload Complete.",
                        //      Toast.LENGTH_SHORT).show();

                        // recursiveDelete(mDirectory1);

                    }

                    // close the streams //
                    fileInputStream.close();
                    dos.flush();
                    dos.close();

                } catch (Exception e) {

                    // dialog.dismiss();
                    e.printStackTrace();

                }
                // dialog.dismiss();

            } // End else block


        } catch (Exception ex) {
            // dialog.dismiss();

            ex.printStackTrace();
        }
        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}



}

And this MyFileFilter.class

public class MyFileFilter implements     FilenameFilter
{

@Override
public boolean accept(File directory, String fileName) {
    if (fileName.endsWith(".txt")) {
        return true;
    }
    return false;
}
}
user5050715
  • 45
  • 10
  • I think using Volley would be better for this. You might find [this](http://stackoverflow.com/questions/32262829/how-to-upload-file-using-volley-library-in-android) SO question helpful with uploading files to server – Steve C. Mar 14 '16 at 02:02
  • Ok but i just want to upload folder all files to server not one file...have u a link that help me? – user5050715 Mar 14 '16 at 02:24
  • Just give me a way to upload all files in folder like sdcard/folder ..how to get all files in this folder? I can uplod one file from this folder but all no – user5050715 Mar 14 '16 at 02:27
  • Have you tried this [link]( http://stackoverflow.com/a/29197167/1055954) – Steve C. Mar 18 '16 at 10:26

0 Answers0