将Multipart-form-data上传到php

时间:2014-11-12 09:37:23

标签: java php android forms upload

我想将任何数据(用户名,描述,...)和照片从我的Android-App上传到我的服务器(PHP脚本)。

通过搜索这个我找到了一个解决方案:How to take a photo and send to HTTP POST request with Android?

单击我的发送按钮,上传过程开始,php脚本工作但$ _POST -Vars为空?有谁能够帮我?我的请求代码:

[...]
case R.id.btnSend:
    String strName = tName.getText().toString();
    String strEmail = tEmail.getText().toString();
    String strDatum = tDatum.getText().toString();
    String strBeschreibung = tBeschreibung.getText().toString();        

    nvPairs = new ArrayList<NameValuePair>(2);
    nvPairs.add(new BasicNameValuePair("sender", "AndoidApp"));
    nvPairs.add(new BasicNameValuePair("name", strName));
    nvPairs.add(new BasicNameValuePair("email", strEmail));
    nvPairs.add(new BasicNameValuePair("datum", strDatum));
    nvPairs.add(new BasicNameValuePair("beschreibung", strBeschreibung));
    nvPairs.add(new BasicNameValuePair("bild", bmpUrl));

    new UploadTask().execute();
}
[...]

private class UploadTask extends AsyncTask<Void, Void, List<String>> {
    @Override
    protected List<String> doInBackground(Void... params) {
        String response = "";
        try {
            response = new MultipartServer().postData(new URL(postUrl), nvPairs);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }       
}

在MultipartServer-Code上我更改了一行:

if ((avatarName = pair.getName()).equals("avatar")) {
    avatarPath = pair.getValue();
}

到此:

if ((avatarName = pair.getName()).equals("bild")) {
    avatarPath = pair.getValue();
}

变量“bmpUrl”和“postUrl”在我的代码中的另一行正确设置。

1 个答案:

答案 0 :(得分:1)

感谢您的帮助!我找到了解决问题的方法。问题出在How to take a photo and send to HTTP POST request with Android?

的脚本中

我把它更改为:

public class MultipartServer {

    private static final String TAG = "MultipartServer";
    private static final String crlf = "\r\n";
    private static final String twoHyphens = "--";
    private static final String boundary = "AaB03x"; //*****
    private String avatarName = null;
    private String avatarPath = null;

    public String postData(URL url, List<NameValuePair> nameValuePairs) throws IOException {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(15000);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        FileInputStream inputStream;
        OutputStream outputStream = connection.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

        for (NameValuePair pair : nameValuePairs) {
            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
            dataOutputStream.writeBytes(
                    "Content-Disposition: form-data; name=\""
                    + URLEncoder.encode(pair.getName(), "UTF-8") 
                    + "\";" + crlf);
            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(URLEncoder.encode(pair.getValue(), "UTF-8"));
            if (pair.getName().equals("bild")) {
                avatarName = pair.getName();
                avatarPath = pair.getValue();
            }
        }

        // Write Avatar (if any)
        if (avatarName != null && avatarPath != null) {
            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
            dataOutputStream
                    .writeBytes("Content-Disposition: form-data; name=\""
                            + avatarName + "\";filename=\""
                            + new File(avatarPath).getName() + "\";" + crlf);
            dataOutputStream.writeBytes(crlf);

            inputStream = new FileInputStream(avatarPath);
            byte[] data = new byte[1024];
            int read;
            while ((read = inputStream.read(data)) != -1)
                dataOutputStream.write(data, 0, read);
            inputStream.close();

            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
        }

        dataOutputStream.flush();
        dataOutputStream.close();

        String responseMessage = connection.getResponseMessage();
        Log.d(TAG, responseMessage);

        InputStream in = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(in, "UTF-8"));

        StringBuilder response = new StringBuilder();
        char[] b = new char[512];
        int read;
        while ((read = bufferedReader.read(b)) != -1) {
            response.append(b, 0, read);
        }

        connection.disconnect();
        Log.d(TAG, response.toString());
        return response.toString();
    }
}