Android 2.2 SDK/JAVA通过HttpUrlConnection上传文件/发布参数时出错
我得到了这个错误,尽管在我的postMap中我明确指定了一个post变量“eventTitle”,到底发生了什么
01-03 11:52:29.758: INFO/System.out(27682): Server response is: Warning: htmlspecialchars(): Invalid multibyte sequence in argument in /home/staging/public_html/system/library/request.php on line 31Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/staging/public_html/index.php:100) in /home/staging/public_html/system/library/session.php on line 11Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/staging/public_html/index.php:100) in /home/staging/public_html/system/library/session.php on line 11Warning: Cannot modify header information - headers already sent by (output started at /home/staging/public_html/index.php:100) in /home/staging/public_html/index.php on line 181Warning: Cannot modify header information - headers already sent by (output started at /home/staging/public_html/index.php:100) in /home/staging/public_html/system/library/currency.php on line 45846
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 楼答案
这看起来像是php方面的问题。您可能正在发送输出,然后稍后在脚本中尝试修改头,但您不能这样做,因为它们已经被发送了