我需要将文件和POST参数发送到php。我发现并复制了来自this solution的代码以发送带文件和参数的POST请求,并且它成功运行。但是当我尝试使用表情符号POST文本时,服务器会返回错误的表情符号文本
使用请求数据和响应的示例:
首先我认为我的php配置不正确,但我用REST实用程序测试了这个请求,服务器用emojis返回了正确的响应。
这是用于测试的简单php代码:
$data = $_POST('data');
echo $data;
我错过了什么?也许我需要在android请求中设置charset?但我需要放在哪里?或者解决方案源代码可能有问题?
答案 0 :(得分:0)
我找到了另一个解决方案,工作正常!
final File uploadFile = new File(file);
try {
final MultipartUtility http = new MultipartUtility(new URL(url));
http.addFormField("someField", "someValue");
http.addFormField("data", data);
http.addFilePart("someFile", uploadFile);
String s = http.finish();
Log.d("response", s);
return;
} catch (IOException e) {
e.printStackTrace();
}
代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class MultipartUtility {
private static final String CRLF = "\r\n";
private static final String CHARSET = "UTF-8";
private static final int CONNECT_TIMEOUT = 15000;
private static final int READ_TIMEOUT = 10000;
private final HttpURLConnection connection;
private final OutputStream outputStream;
private final PrintWriter writer;
private final String boundary;
private final URL url;
public MultipartUtility(final URL url) throws IOException {
this.url = url;
boundary = "---------------------------" + System.currentTimeMillis();
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(READ_TIMEOUT);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", CHARSET);
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET),
true);
}
public void addFormField(final String name, final String value) {
writer.append("--").append(boundary).append(CRLF)
.append("Content-Disposition: form-data; name=\"").append(name)
.append("\"").append(CRLF)
.append("Content-Type: text/plain; charset=").append(CHARSET)
.append(CRLF).append(CRLF).append(value).append(CRLF);
}
public void addFilePart(final String fieldName, final File uploadFile)
throws IOException {
final String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(CRLF)
.append("Content-Disposition: form-data; name=\"")
.append(fieldName).append("\"; filename=\"").append(fileName)
.append("\"").append(CRLF).append("Content-Type: ")
.append(URLConnection.guessContentTypeFromName(fileName)).append(CRLF)
.append("Content-Transfer-Encoding: binary").append(CRLF)
.append(CRLF);
writer.flush();
outputStream.flush();
try {
final FileInputStream inputStream = new FileInputStream(uploadFile);
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
}finally {
}
writer.append(CRLF);
}
public void addHeaderField(String name, String value) {
writer.append(name).append(": ").append(value).append(CRLF);
}
public String finish() throws IOException {
writer.append(CRLF).append("--").append(boundary).append("--")
.append(CRLF);
writer.close();
final int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
throw new IOException(String.format("{0} failed with HTTP status: {1}",
url, status));
}
try {
final InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
StringBuffer response = new StringBuffer();
String inputLine = in.readLine();
while (inputLine != null) {
response.append(inputLine);
inputLine = in.readLine();
if (inputLine != null)
response.append('\r');
}
in.close();
isr.close();
return response.toString();
/*
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
bytes.write(buffer, 0, bytesRead);
}
return bytes.toByteArray();
*/
} finally {
connection.disconnect();
}
}
}