вы должны работать как этот
public void connectForMultipart() throws Exception {
con = (HttpURLConnection) (new URL(url)).openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.connect();
os = con.getOutputStream();
}
public void addFormPart(String paramName, String value) throws Exception {
writeParamData(paramName, value);
}
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
os.write((delimiter + boundary + "\r\n").getBytes());
os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes());
os.write(("Content-Type: application/octet-stream\r\n" ).getBytes());
os.write(("Content-Transfer-Encoding: binary\r\n" ).getBytes());
os.write("\r\n".getBytes());
os.write(data);
os.write("\r\n".getBytes());
}
public void finishMultipart() throws Exception {
os.write((delimiter + boundary + delimiter + "\r\n").getBytes());
}
обновление
для получения ответа
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
while ((sResponse = reader.readLine()) != null)
{
s = s.append(sResponse);
}
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
return s.toString();
}else
{
return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Вместо изображения вы можете отправить что-нибудь, вы хотели бы .. .Если он создает некоторые черты, то вы должны разделить большой файл на мелкие части, а затем попытаться отправить ing. И присоединяйтесь к этой небольшой части на сервере.
Вы не указали, как получить строку. как я могу получить строку? как я могу получить запрос в виде строки? – migs
, пожалуйста, уточните мой обновленный ответ :) – Gattsu
Как вы можете сказать, что 'response.getEntity(). GetContent()' вернет 'данные' в' addFilePart', а не 'значение' в' addFormPart'? – migs