java httpurlconnection "failed to read: line to long:"

java httpurlconnection "failed to read: line to long:"

我正在使用 httpurlconnection 将文件上传到不同的服务器。

对于某些主机,以下代码可以正常工作,我可以上传文件,但是当我尝试上传到另一台主机时,我收到一条错误消息

"failed to read: line too long: \nContent-Disposition: form-data; name...

这个错误是什么意思?我找不到 google 的任何提示。希望你能帮忙。 :)

private void upload(){

    response = "";

    DataOutputStream outputStream = null;
    InputStream inputStream = null;

    String twoHyphens = "--";
    String boundary =  "--------"+Long.toString(System.currentTimeMillis());
    String lineEnd = "\n";

    try {
        File file = getFile();

        StringBuffer sb = new StringBuffer();

        sb.append(twoHyphens + boundary + lineEnd);
        sb.append("Content-Disposition: form-data; name=\"file1\"; filename=\"" + getFilename() +"\"" + lineEnd);
        sb.append("Content-Type: " + getMime() + lineEnd);
        sb.append("Content-Transfer-Encoding: binary" + lineEnd);
        sb.append(lineEnd);

        FileInputStream fileInputStream = new FileInputStream(file);

        connection = (HttpURLConnection) url.openConnection();


        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setFixedLengthStreamingMode((uploadContainer.getFilesize() + sb.length() + lineEnd.length() + twoHyphens.length() + boundary.length() + twoHyphens.length() + lineEnd.length()));


        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);

        outputStream = new DataOutputStream(connection.getOutputStream());      

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"file1\"; filename=\"" + getFilename() +"\"" + lineEnd);
        outputStream.writeBytes("Content-Type: " + getMime() + lineEnd);
        outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        outputStream.writeBytes(lineEnd);       


        int bytesRead = -1;
        byte[] buffer = new byte[4096];
        boolean cancelled = false;
        while ((bytesRead = fileInputStream.read(buffer)) > 0){
            outputStream.write(buffer, 0, bytesRead);

            if(Thread.currentThread().isInterrupted()){
                cancelled = true;
                break;
            }
        }
        if(cancelled == false){
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);


            outputStream.flush();

            inputStream = connection.getInputStream();

            response = convertStreamToString(inputStream);  

            inputStream.close();

        } else {
            // ...
        }
        fileInputStream.close();

    } catch(Exception e) {/e.printStackTrace();
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {e.printStackTrace();
        }
    }
}
String lineEnd = "\n";

问题就在这里。 HTTP 中的行结束符定义为 \r\n,而不是 \n.