0

我正在尝试将视频上传到服务器,在这里我将视频存储在资产文件夹中,并尝试上传。我使用了以下代码:

private void uploadVideo() {


              String upLoadServerUri = "http://xxxxx/video_upload.php";             
              // String [] string = sourceFileUri;
              String fileName = "file:///android_asset/clip0003";


              HttpURLConnection conn = null;
              DataOutputStream dos = null;
              DataInputStream inStream = null;
              String lineEnd = "\r\n";
              String twoHyphens = "--";
              String boundary = "*****";
              int bytesRead, bytesAvailable, bufferSize;
              int serverResponseCode=1;
              byte[] buffer;
              int maxBufferSize = 1 * 1024 * 1024;
              String responseFromServer = "";

              File sourceFile = new File(fileName);
              if (!sourceFile.isFile()) {
               Log.e("Huzza", "Source File Does not exist");

              }

            try { // open a URL connection to the Servlet

               FileInputStream fileInputStream = new FileInputStream(new File(fileName));
               URL url = new URL(upLoadServerUri);
               conn = (HttpURLConnection) url.openConnection(); // Open a HTTP  connection to  the URL
               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("video", fileName);
               dos = new DataOutputStream(conn.getOutputStream());

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

               bytesAvailable = fileInputStream.available(); // create a buffer of  maximum size
               Log.i("Huzza", "Initial .available : " + bytesAvailable);

               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();

               Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
               // close streams
               Log.i("Upload file to server", fileName + " File is written");
               fileInputStream.close();
               dos.flush();
               dos.close();
              } catch (MalformedURLException ex) {
               ex.printStackTrace();
               Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
              } catch (Exception e) {
               e.printStackTrace();
              }
            //this block will give the response of upload link
              try {
               BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
               String line;
               while ((line = rd.readLine()) != null) {
                Log.i("Huzza", "RES Message: " + line);
               }
               rd.close();
              } catch (IOException ioex) {
               Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
              }


             } // end upLoad2Server

但由于以下错误,我无法上传!!任何人都可以帮助我。谢谢!

错误:

03-20 18:09:07.033: E/AndroidRuntime(1529): FATAL EXCEPTION: main
03-20 18:09:07.033: E/AndroidRuntime(1529): java.lang.NullPointerException
03-20 18:09:07.033: E/AndroidRuntime(1529):     at com.example.galleryframe.videoupload.uploadVideo(videoupload.java:233)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at com.example.galleryframe.videoupload.access$0(videoupload.java:150)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at com.example.galleryframe.videoupload$3.onClick(videoupload.java:114)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at android.view.View.performClick(View.java:2485)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at android.view.View$PerformClick.run(View.java:9080)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at android.os.Handler.handleCallback(Handler.java:587)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at android.os.Looper.loop(Looper.java:123)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at java.lang.reflect.Method.invoke(Method.java:507)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 18:09:07.033: E/AndroidRuntime(1529):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

在不知道哪一行是 233 的情况下,我会猜测并说问题行是

FileInputStream fileInputStream = new FileInputStream(new File(fileName));

filename被定义为 URI,但您正尝试将输入流创建为File. 我怀疑这个输入流是null. 使用调试器逐步确认。

编辑:如果要从assets文件夹中读取文件,请使用getAssets()方法:

String fileName = "clip0003";
InputSteam fileInputSteam = new InputStreamReader(getAssets().open(filename));

请注意,此代码假定您在Activity扩展的类或其他类中Context。如果没有,您将需要访问 aContext和 call context.getAssets()

于 2013-03-20T12:47:11.233 回答