Android 2.2 SDK / JAVA - 通过HttpUrlConnection错误上传文件/发布参数

时间:2011-01-03 18:58:36

标签: java android http-headers

我收到了这个错误,虽然在我的postMap中我明确指定了一个post var“eventTitle”,到底发生了什么?:

  

01-03 11:52:29.758:   INFO / System.out(27682):服务器   回复是:警告:   htmlspecialchars():无效的多字节   参数中的序列    /home/staging/public_html/system/library/request.php 的   在线 31 警告:   session_start():无法发送会话   cookie - 已经发送的标头   (输出始于   /home/staging/public_html/index.php:100)   在    /home/staging/public_html/system/library/session.php 的   在线 11 警告:   session_start():无法发送会话   缓存限制器 - 已发送的标头   (输出始于   /home/staging/public_html/index.php:100)   在    /home/staging/public_html/system/library/session.php 的   在线 11 警告:   无法修改标题信息 -   已经发送的标题(输出   开始于   /home/staging/public_html/index.php:100)   在    /home/staging/public_html/index.php 的   在线 181 警告:   无法修改标题信息 -   已经发送的标题(输出   开始于   /home/staging/public_html/index.php:100)   在    /home/staging/public_html/system/library/currency.php 的   在线    45 846

public String postHTTPMultipart(LinkedHashMap<String, String> postMap, String apiCall, String token, String imagePath) {

     private String header1 = "api-key";
     private String apiKey = "xxx";
     private String header2 = "User-agent";
    private String headerValue2 = "Testing";
     private String header3 = "Accept:";
     private String headerValue3 = "application/json";
     private String header4 = "session-token";

        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;

        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

        int bytesRead, bytesAvailable, bufferSize;

        byte[] buffer;

        int maxBufferSize = 1 * 1024 * 1024;

        try {

            // ------------------ CLIENT REQUEST

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

            //open a URL connection to the Servlet
            URL url = new URL(apiCall);

            //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.addRequestProperty(header1, apiKey);
            conn.addRequestProperty(header2, headerValue2);
            conn.addRequestProperty(header3, headerValue3);
            conn.addRequestProperty(header4, token);

            conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);

            //add post parameters
            String urlParameters = null;
            for (Entry<String, String> entry : postMap.entrySet()) {

                String key = entry.getKey();
                String value = entry.getValue();

                String append = key + "=" + URLEncoder.encode(value, "UTF-8") + "&";

                if (urlParameters != null) {
                    urlParameters = urlParameters + append;
                } else {
                    urlParameters = append;
                }

            }

            urlParameters = urlParameters.substring(0, urlParameters.length() - 1);

            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"file\";" + " eventImage=\"" + imagePath + "\"" + 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);

            //add post parameters here?
            dos.writeBytes(twoHyphens + boundary + lineEnd + urlParameters + lineEnd);

            // close streams
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {
            System.out.println("From ServletCom CLIENT REQUEST:" + ex);
        }

        catch (IOException ioe) {
            System.out.println("From ServletCom CLIENT REQUEST:" + ioe);
        }

        // ------------------ read the SERVER RESPONSE

        String response = null;
        try {
            inStream = new DataInputStream(conn.getInputStream());
            while ((response = inStream.readLine()) != null) {
                System.out.println("Server response is: " + response);
                System.out.println("");
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.e("IOException", "Exception", ioex);
            System.out.println("From (ServerResponse): " + ioex);

        }

        return response;
}

1 个答案:

答案 0 :(得分:0)

看起来像是php方面的问题。您可能正在发送输出,然后在脚本中尝试修改标题,但您不能这样做,因为它们已经被发送。