【发布时间】:2015-09-03 07:55:58
【问题描述】:
对不起我的英语。我尝试使用 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;
}
【问题讨论】:
-
这是关于同一问题的第二篇文章。您没有以任何方式更改文本。您甚至没有展示如何调用这些函数。您在另一篇文章的评论中所做的。您没有解释为什么您的示例中没有图像内容。你应该给点信息更多。还有关于你的服务器。您并没有告诉服务器接收到什么。哪些 dioes 不能完全正常工作?
-
@greenapps 感谢您的回答,我更新了我的问题
标签: java android post multipartform-data