2016-03-20 33 views
-2

アンドロイドからノードサーバーに画像を送信しようとしています。私が何をしたか:Android - Node.js経由のマルチパートリクエスト

 try{ 
      //------------------ CLIENT REQUEST 
      FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 
      // 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=\"image_upload\";originalFilename=\"" + params[1] + "\";path=\"" + params[0] + "\"" + lineEnd); // originalFilename is the Name of the File to be uploaded 
      dos.writeBytes(lineEnd); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 
      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); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } 

私はimage_uploadファイルfiles.image_upload[0].originalFilenameとしてノード内のパラメータとfiles.image_upload[0].path

問題がimage_upload[0]で起こっているを取得しています。サーバーがクラッシュするというメッセージが表示される TypeError: Cannot read property '0' of undefined

+0

私が次の質問を改善できるように、downvoteの理由を述べることができれば、本当に役に立ちます。 –

答えて

0

イメージパスファイルにMIMEタイプを指定します。

0

これにコードの一部を置き換えて、現在動作しています。

 dos.writeBytes("Content-Disposition: form-data; name=\"image_upload\"; filename=\""+params[1]+"\""+lineEnd); 
     dos.writeBytes("Content-Type: \"undefined\""+lineEnd); 
     dos.writeBytes(lineEnd); 
関連する問題