-2

I need to upload file on shared folder inside lampp ht-docs directory.

The file is inside internal storage. I have also path of that file with me.

I have tried some of the solutions but it is not moving file to that shared folder inside lampp ht-docs.

I have the URL like this http://192.168.1...../OrderFiles/. OrderFiles is the shared folder inside lampp htdocs directory. I want to upload file here from file path of internal storage.

Here is what i have tried.

public void uploadLocal() {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        File fileName = new File(DBController.lastXmlPathLocal);
        Log.w("getAbsolutePath", fileName.getAbsolutePath());
        Log.w("getPath", fileName.getPath());
        String existingFileName = fileName.getAbsolutePath();
        Log.w("existingFileName", existingFileName);
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        String responseFromServer = "";
        String urlString = "http://192.168.1......./OrderFiles/";
        Log.w("urlString", urlString);
        try {
            //------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(fileName.getPath()));
            // open a URL connection to the Servlet
            URL url = new URL(urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + 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)
            Log.v("info", ".size." + bytesRead);
            for (int n1 = 0; n1 < bytesRead; n1++) {


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

            }

            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Log.v("info", "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.v("info", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.v("info", "error: " + ioe.getMessage(), ioe);
        }
        /*//------------------ read the SERVER RESPONSE
        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                Log.v("info", "Server Response " + str);
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.v("info", "error: " + ioex.getMessage(), ioex);
        }*/
    }

The above code showing File is written in logcat but inside that folder not showing any of the file.

Is their any other solution to integrate this ?

Any help would be greatly appreciated.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • @Downvoter please specify the reason to downvoting. What's need to be improve in question. – Jay Rathod Mar 29 '17 at 08:39
  • `//------------------ read the SERVER RESPONSE`. You should do that. And tell us the response. – greenapps Mar 29 '17 at 08:54
  • `String urlString = "http://192.168.1......./OrderFiles/";`. What kind of php script is behind thst url? – greenapps Mar 29 '17 at 08:55
  • @greenapps In server response it giving me `java.io.FileNotFoundException: http://192.168.1....../OrderFiles/`. And my file path is `/storage/emulated/0/FolderName/Ord_0002.xml`. – Jay Rathod Mar 29 '17 at 08:59
  • @greenapps Not any php script is their behind this `URL`. I need to upload without that. is it possible to do with what i am trying currently ? – Jay Rathod Mar 29 '17 at 09:00
  • Lamp without php? Then what is it that you are using? – greenapps Mar 29 '17 at 09:01
  • @greenapps Actually i have shared folder inside htdocs --> lampp directory as i need to upload file their directly without using any php script. Is it possible to do without any php script ? – Jay Rathod Mar 29 '17 at 09:02
  • You can have as many subfolders in htdocs as you want. Irrelevant at the moment. But you send your file to a lamp server. And hence you have to use a php script. Without a php script its impossible as you have seen. – greenapps Mar 29 '17 at 09:07
  • `The above code showing File is written in logcat`. We do not believe that as you have a catch as you ststed later in a comment. – greenapps Mar 29 '17 at 09:09
  • @greenapps In above comment i have put a server response as you mentioned it showing me `java.io.FileNotFoundException: http://192.168.1....../OrderFiles/.` And my file path is `/storage/emulated/0/FolderName/Ord_0002.xml.` is their anything wrong with this ? – Jay Rathod Mar 29 '17 at 09:10
  • You are only repeating yourself without giving further info. Moreover its unclear where you see that server response. I would think as exception in a catch block. Which one? And if not then where and how exactly? – greenapps Mar 29 '17 at 09:12
  • @greenapps let me know exactly what further info you need. I have repeated myself because i thought might you haven't look into that comment. And yes the server response giving exception in catch block. – Jay Rathod Mar 29 '17 at 09:19
  • And you are not telling in which one!? I need no further info. And you know now that its impossible without php script. – greenapps Mar 29 '17 at 09:21
  • @greenapps So i need php script for sure to move file in that shared folder ? no any other solutions ? – Jay Rathod Mar 29 '17 at 09:27
  • L.A.M.P. = Linux, Apache, MySQL, PHP ! Yes, it seems that you really need to use PHP. – Budius Mar 29 '17 at 09:49

1 Answers1

0

First you need a php script to upload a file to begin with. And after that the php script should contain code to move the uploaded file to your wanted folder. And that is how it goes. And that is how you should do it.

greenapps
  • 11,154
  • 2
  • 16
  • 19