抱歉我的英文。我尝试使用post和multipart / form-data将图像发送到服务器。我花了很多次,尝试修复我的代码,但都无济于事。这是我需要作为图像发送的示例:
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="image"; filename="imageName.jpeg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
这是我的第一个版本:
public static String executeHttpLoadImage(String url, String nameValueF, String paramValueF, String nameValueS, String paramValueS,
File file, String fileName) throws Exception {
BufferedReader in = null;
try {
String BOUNDARY= "----WebKitFormBoundaryE19zNvXGzXaLvS5C";
HttpClient client = createHttpClient();
HttpPost request = new HttpPost(); //post
request.setURI(new URI(url));
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY); //set header
request.setHeader(nameValueF, paramValueF); //set header
request.setHeader(nameValueS, paramValueS); //set header
//set multipart
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
try {
entity.addPart("file01", new FileBody(file));
request.addHeader("Accept-Encoding", "gzip, deflate");
} catch(Exception e) {
Log.e("error", e.toString());
}
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.setEntity(entity);
//set multipart
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我的第二个版本:
public static String executeHttpLoadImage(String url, String nameValueF, String paramValueF, String nameValueS, String paramValueS,
File file, String fileName) throws Exception {
BufferedReader in = null;
try {
String BOUNDARY= "----WebKitFormBoundaryE19zNvXGzXaLvS5C";
HttpClient client = createHttpClient();
HttpPost request = new HttpPost(); //post
request.setURI(new URI(url));
request.setHeader("Content-Type", "form-data; name=image; filename=imageName.jpeg"); //set header
request.setHeader(nameValueF, paramValueF); //set header
request.setHeader(nameValueS, paramValueS); //set header
//set multipart
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
try {
entity.addPart("file01", new FileBody(file));
} catch(Exception e) {
Log.e("error", e.toString());
}
//set multipart
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
但我的所有版本都不起作用:(
UPD:
我在asynkTask中使用这个:
protected String doInBackground(String... params) {
JSONObject json = null;
try{
json = new JSONObject(CustomHttpClient.executeHttpLoadImage(
"https://example.net", "id", "123",
"key", "123",
fileName, "image1"));
Log.e("load image", json.toString());
}catch(Exception e) {
Log.e("Load image", e.toString());
}
return null;
}
fileName - 我从图库中选择的文件,我会像这样:
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
try{
Bitmap myBitmap = decodeUri(selectedImage);
//set
fileName = persistImage(myBitmap, "1123123");
//load image
new LoadImageToServer().execute();
imageRoom.setImageBitmap(myBitmap);
} catch(Exception e) {
Log.e("Image", e.toString());
}
//code
//method return bitmap -> file
private File persistImage(Bitmap bitmap, String name) {
File filesDir = context.getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e("Error writing bitmap", e.toString());
}
return imageFile;
}
答案 0 :(得分:0)
我建议使用REST的库“Retrofit”。使他的http客户端不是“美丽”选项。