使用http在android上传视频

时间:2014-08-26 17:45:43

标签: php android http

伙计们,请帮助我..我在Android开发中的新手..

我参考androidhive的链接创建和保存视频.....并使用http上传视频我参考http://sunil-android.blogspot.in/2013/09/file-upload-on-php-server-in-android.html ...

这是我的xml设计 http://i1347.photobucket.com/albums/p701/abhinavdubey/Untitled_zps56def610.png 视频和打开视频工作正常,点击上传后我得到“http响应好了:200”,最后显示senduseractionevent()mview == null ..否则视频不保存在服务器端..... ....作为新手代码不合适......请帮助我..

这是我的代码... thnx提前..

    private int doFileUpload(String sourcefile)  throws ParseException,IOException

    {
    // TODO Auto-generated method stub

        String fileName = sourcefile;
        DataInputStream inStream = null;
        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* 1024 ; 
        File sourceFileuri = new File(sourcefile);
       // String responseFromServer = "";
        String urlString = "http://192.168.1.115/dropouts/upload.php";

        if (!sourceFileuri.isFile()) {

             dialog.dismiss(); 

             Log.e("uploadFile", "Source File not exist :"+selectedPath);

             runOnUiThread(new Runnable() {
                 public void run() {


                     //messageText.setText("Source File not exist :"+ imagepath);
                 }
             }); 

             return 0;

        }
        else
        {
             try { 

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

                 // 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);
           //ICT     // conn.setRequestProperty("uploadedfile", fileName); 
                 conn.setRequestProperty("file", fileName); 
                 dos = new DataOutputStream(conn.getOutputStream());

                 dos.writeBytes(twoHyphens + boundary + lineEnd); 
   /*ICT*/            /*  dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                                           + fileName + "\"" + lineEnd);*/

                   dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
                 + fileName + "\"" + 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)
                 serverResponseCode = conn.getResponseCode();
                 String serverResponseMessage = conn.getResponseMessage();

    /*ICT*/            /* Log.i("uploadFile", "HTTP Response is : "
                         + serverResponseMessage + ": " + serverResponseCode);*/

                 Log.i("File", "HTTP Response is : "
                 + serverResponseMessage + ": " + serverResponseCode);

                 if(serverResponseCode == 200){

                     runOnUiThread(new Runnable() {
                          public void run() {
                              String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                    +" F:/wamp/wamp/www/uploads";
                              //messageText.setText(msg);
                              Toast.makeText(MainActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                          }
                      });                
                 }    

                 //close the streams //

                 fileInputStream.close();
                 dos.flush();
                 dos.close();
                 dialog.dismiss();

            } catch (MalformedURLException ex) {

                dialog.dismiss();  
                ex.printStackTrace();

                runOnUiThread(new Runnable() {
                    public void run() {
                        //messageText.setText("MalformedURLException Exception : check script url.");
                        Toast.makeText(MainActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                    }
                });

                Log.e("Upload file to server", "error: " + ex.getMessage(), ex);  
            } catch (Exception e) {

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

                runOnUiThread(new Runnable() {
                    public void run() {
                       // messageText.setText("Got Exception : see logcat ");
                        Toast.makeText(MainActivity.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();
                    }
                });
                Log.e("Upload file to server Exception", "Exception : "  + e.getMessage(), e);  
            }

            try
            {
                inStream = new DataInputStream ( conn.getInputStream() );
                String str;
                while (( str = inStream.readLine()) != null)
                {
                  System.out.println(str);
                }
                inStream.close();
              }catch (IOException ioex)
              {
                System.out.println("Error: "+ioex);


               }

             }
        return serverResponseCode;
    }

0 个答案:

没有答案