机器人通过MultipartEntity发送图像到服务器 - 设置内容类型? [英] Android sending image to Server via MultipartEntity - setting Content-type?
问题描述
我读过很多很多职位有关从Android应用程序和内容类型明智发送图像到服务器,它们被分为三大类:
一)他们不设置内容类型在所有的,可能在某种程度上他们的code ++工程
二)使用的是德precated方法
C),他们使用的是从我选择了一个完全不同的方法。
我想发送文件转移到服务器并将其存储一个文件夹中。
我的code是一个总的拼凑,我设法阅读大量的文章和文章后,拿出一个屠宰场工作,这里是:
公共无效uploadImageToServer(字符串的ImagePath)抛出异常{
尝试 {
//设置HTTP处理程序
HttpClient的=新DefaultHttpClient();
localContext =新BasicHttpContext(); //为什么我需要这个?
postRequest =新HttpPost(http://asd.com/asd.php);
//postRequest.addHeader("Content-type,图像/ JPEG); // - 这没有工作
//处理文件
ByteArrayOutputStream byteArrayOutputStream =新ByteArrayOutputStream();
位= BitmapFactory.de codeFILE(的ImagePath);
bitmap.com preSS(比较pressFormat.JPEG,75,byteArrayOutputStream);
byte []的byteData = byteArrayOutputStream.toByteArray();
//字符串strData是= Base64.en codeToString(数据,Base64.DEFAULT); //我不知道为什么林这样做
ByteArrayBody byteArrayBody =新ByteArrayBody(byteData,图像); //第二个参数是图像的名称(// TODO我怎么做它使用的图像文件名?)
//送包
multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart(uploaded_file,byteArrayBody);
postRequest.setEntity(multipartEntity.build());
//得到响应。我们将与它在处理onPostExecute。
响应= httpClient.execute(postRequest,localContext);
bitmap.recycle();
}赶上(例外五){
// TODO自动生成的catch块
e.printStackTrace();
}
}
和错误是:
致命异常:AsyncTask的#1
java.lang.RuntimeException的:执行doInBackground时出错()
android.os.AsyncTask $ 3.done(AsyncTask.java:200)
java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:274)
java.util.concurrent.FutureTask.setException(FutureTask.java:125)
java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:308)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
java.lang.Thread.run(Thread.java:1019)
致:java.lang.NoClassDefFoundError的:org.apache.http.entity.ContentType
org.apache.http.entity.mime.content.ByteArrayBody< INIT>(ByteArrayBody.java:67)
org.apache.http.entity.mime.content.ByteArrayBody< INIT>(ByteArrayBody.java:87)
如果您使用的是图书馆,你需要把它变成 /库文件夹。
编辑:
下载httpmime,的HttpCore和HttpClient的从图书馆 http://hc.apache.org/downloads.cgi
I've read many many posts about sending an image to the server from an Android app and Content-type-wise, they are divided in three categories:
a) they dont set the content-type at all and probably somehow their code works
b) they are using deprecated methods
c) they are using completely different approach from the one I've selected.
I would like to send the file over to the server and store it in a folder.
My code is a total patchwork, a butchery job that I managed to come up with after reading lots of posts and articles, here is it:
public void uploadImageToServer(String imagePath) throws Exception {
try {
// set the http handlers
httpClient = new DefaultHttpClient();
localContext = new BasicHttpContext(); // why do I need this?
postRequest = new HttpPost("http://asd.com/asd.php");
//postRequest.addHeader("Content-type", "image/jpeg"); // - this didnt work
// deal with the file
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap = BitmapFactory.decodeFile(imagePath);
bitmap.compress(CompressFormat.JPEG, 75, byteArrayOutputStream);
byte[] byteData = byteArrayOutputStream.toByteArray();
//String strData = Base64.encodeToString(data, Base64.DEFAULT); // I have no idea why Im doing this
ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image (//TODO HOW DO I MAKE IT USE THE IMAGE FILENAME?)
// send the package
multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("uploaded_file", byteArrayBody);
postRequest.setEntity(multipartEntity.build());
// get the response. we will deal with it in onPostExecute.
response = httpClient.execute(postRequest, localContext);
bitmap.recycle();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And the error is:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
android.os.AsyncTask$3.done(AsyncTask.java:200)
java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
java.util.concurrent.FutureTask.setException(FutureTask.java:125)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:67)
org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:87)
If you are using a library, you need to put it into /libs folder.
EDIT:
download httpmime, httpcore and httpclient library from http://hc.apache.org/downloads.cgi
这篇关于机器人通过MultipartEntity发送图像到服务器 - 设置内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!