0

我正在制作一个 HttpRequest 来上传 3 张图片和一些参数。我已经给出了下面的代码。当我向 localhost 发出请求时一切正常,但是当我向外部服务器发出请求时,服务器返回CODE 400

这是我的 httprequest 代码

public class ExpenseUpload {
   private int serverResponseCode;
   SessionManager sessionManager;

public String uploadTDdetails(Context context, String edit_expense, String strtripid, String amt1, 
String amt2, String amt3, String exp1, String exp2, String exp3, String imagePath, String 
imagePath1, String imagePath2) {
    sessionManager = new SessionManager();
    HttpURLConnection conn = null;
    DataOutputStream dos;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File expenseFile = null, expenseFile1 = null, expenseFile2 = null;
    if (!imagePath.isEmpty()) {
        expenseFile = new File(imagePath);
    }
    if (!imagePath1.isEmpty()) {
        expenseFile1 = new File(imagePath1);
    }
    if (!imagePath2.isEmpty()) {
        expenseFile2 = new File(imagePath2);
    }
    try {
        FileInputStream expenseInputStream = null, expenseInputStream1 = null, expenseInputStream2 = 
null;
        if (expenseFile != null) {
            expenseInputStream = new FileInputStream(expenseFile);
        }
        if (expenseFile1 != null) {
            expenseInputStream1 = new FileInputStream(expenseFile1);
        }
        if (expenseFile2 != null) {
            expenseInputStream2 = new FileInputStream(expenseFile2);
        }
        URL url;
        url = new URL(BASE_URL + com.istrides.allenstrans.Retrofit.URL.editexpense);
        Log.i("senorita", url.toString());
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("authorization", "Bearer" + 
sessionManager.getSharedPreferencesValues(context, "Authkey"));
        Log.d("in2in", " " + sessionManager.getSharedPreferencesValues(context, "Authkey"));
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        conn.setRequestProperty("filename", "expenses");
        dos = new DataOutputStream(conn.getOutputStream());
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"api_method\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(edit_expense);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"trip_id\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(strtripid);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"expenses_1_amt\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(amt1);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"expenses_2_amt\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(amt2);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"expenses_3_amt\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(amt3);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"expenses_1\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(exp1);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"expenses_2\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(exp2);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        // add parameters
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"expenses_3\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // assign value
        dos.writeBytes(exp3);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        if (expenseInputStream != null) {
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"expenses_1_file\";filename=\"" + 
"expense1.jpg" + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            bytesAvailable = expenseInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = expenseInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = expenseInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = expenseInputStream.read(buffer, 0, bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            expenseInputStream.close();
        }
        if (expenseInputStream1 != null) {
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"expenses_2_file\";filename=\"" + 
"expense2.jpg" + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            bytesAvailable = expenseInputStream1.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = expenseInputStream1.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = expenseInputStream1.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = expenseInputStream1.read(buffer, 0, bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            expenseInputStream1.close();
        }
        if (expenseInputStream2 != null) {
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"expenses_3_file\";filename=\"" + 
"expense3.jpg" + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            bytesAvailable = expenseInputStream2.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = expenseInputStream2.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = expenseInputStream2.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = expenseInputStream2.read(buffer, 0, bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            expenseInputStream2.close();
        }
        serverResponseCode = conn.getResponseCode();
        Log.d("server_response", "Service response Code " + serverResponseCode);
        dos.flush();
        dos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.d("in2in", " serverResponseCode " + serverResponseCode);
    if (serverResponseCode == 200) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
        } catch (IOException ioex) {
        }
        Log.d("in2in", "img upload" + sb.toString());
        return sb.toString();
    } else {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
        } catch (IOException ioex) {
        }
        Log.d("in2in", "img upload" + sb.toString());
        return "Could not upload";
    }
}
}

仅供参考,我已经在服务器中授予了文件的写权限。

请帮我解决这个问题。

4

0 回答 0