0

httpファイル転送は初めてです。androidsdcardからサーバーにファイルを送信したい。そのために私は以下のコードを試しました。バイトをjson文字列に変換し、サーバーに送信しました。しかし、サーバー側では受信できません。サーバー側でjspを使用しています。しかし、これを行うにはいくつかの効率的な方法があるはずです。いくつかのアイデアを教えてください。

 HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2:8084/httptest");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        String encodedString = convertURL(jsonString);
        nameValuePairs.add(new BasicNameValuePair("wavfil", encodedString));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {

            String responseString = EntityUtils.toString(entity, "UTF-8"); 
            Toast.makeText(this, responseString, Toast.LENGTH_SHORT).show();
            tv.setText(responseString);
            Log.d("HTTP LOG", responseString);

        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

jsp

<%

String value = request.getParameter("wavfil");

byte[] wavByte = value.getBytes();

FileOutputStream fos = new FileOutputStream("/TESTFILE.wav");
fos.write(wavByte, 0, wavByte.length);
if (wavByte != null) {
    out.println("Success");
} else {
    out.println("Failed");
}

%>

4

1 に答える 1

1

下の行を追加

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.x.x.x:8084/httptest");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    String encodedString = convertURL(jsonString);
    nameValuePairs.add(new BasicNameValuePair("wavfil", encodedString));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Create and attach file to the Post
File file = new File("pathto your file"); //replace with actual path
MultipartEntity entity = new MultipartEntity();

httppost.setEntity(entity);

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    entity.addPart("file", new FileBody(file));
    if (entity != null) {

        String responseString = EntityUtils.toString(entity, "UTF-8"); 
        Toast.makeText(this, responseString, Toast.LENGTH_SHORT).show();
        tv.setText(responseString);
        Log.d("HTTP LOG", responseString);

    }
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
于 2013-01-31T06:18:24.300 に答える