每次获取响应代码400,但如果使用fidller命中,API工作正常

时间:2016-02-18 09:46:53

标签: java android file-upload

我正在使用此代码将文件上传到我的服务器,但每次都返回HTTP响应代码400。如果我使用Fiddler命中API,它可以正常工作。

public int uploadFile() {

    int serverResponseCode = 0;


    HttpURLConnection conn = null;

    DataOutputStream dos = null;
    String lineEnd = "\n";
    String boundary = "---------------------------acebdf13572468";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;

    int maxBufferSize = 1 * 1024 * 1024;

    File sourceFile = new File(sourceFileUri);


    try {
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        StringBuilder urlString = new StringBuilder(URLClass.getURLClassObject(context).selectURL());
        urlString.append("Api/AssetsAPI/SaveAssetAttachments");
        Log.e("urlString", urlString.toString());
        URL url = new URL(urlString.toString());

        // Open a HTTP  connection to  the URL
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true); // Allow Inputs
        conn.setDoOutput(true); // Allow Outputs
        conn.setUseCaches(false); // Don't use a Cached Copy
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("uploaded_file", sourceFileUri);

        if (((ApplicationClass) context.getApplicationContext()).getTOKEN() != null)
            conn.setRequestProperty("Token", ((ApplicationClass) context.getApplicationContext()).getTOKEN());
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\"; filename=\"" + sourceFileUri + "\"");
        dos.writeBytes(lineEnd);
        //dos.writeBytes(lineEnd + boundary + 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(boundary + lineEnd);


        dos.writeBytes("Content-Disposition: form-data; name=\"assetId\"");
        dos.writeBytes(lineEnd);
        dos.writeBytes(assetId);
        dos.writeBytes(lineEnd);
        dos.writeBytes(lineEnd + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"documentTypeId\"");
        dos.writeBytes(lineEnd);
        Log.e("docId", docId);
        dos.writeBytes(docId);
        dos.writeBytes(lineEnd);
        dos.writeBytes(lineEnd + boundary);


        // Responses from the server (code and message)
        serverResponseCode = conn.getResponseCode();
        String serverResponseMessage = conn.getResponseMessage();

        Log.e("HTTP Response is : "
                , serverResponseMessage + ": " + serverResponseCode);

        if (serverResponseCode == 200) {
        }

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

    } catch (MalformedURLException ex) {

        ex.printStackTrace();

    } catch (Exception e) {
    }
    return serverResponseCode;

}

0 个答案:

没有答案