张贴到服务器的单个图像局部 [英] Posting a Single image to server locally
本文介绍了张贴到服务器的单个图像局部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个简单的图片上传到本地服务器
我所试图做的 ::我想一个图像上传到服务器上的按钮,点击
- 我已经提到此链接
- 但我不知道如何实施
POSTDATA()
功能
链接我试图张贴图片:
http://10.0.2.2/Details/
使用名称键
的图片
MainActivity.java
公共类MainActivity延伸活动{ 按钮提交;
ProgressDialog pDialog; ImageView的ImageView的; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 提交=(按钮)findViewById(R.id.SUBMIT_BUTTON_ID); ImageView的=(ImageView的)findViewById(R.id.imageView1); submit.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
新MainTest()执行();
}
});
}
公共无效POSTDATA(){
} / **
*方法张贴图像到服务器。
* U将不得不改变,这将接受图像数据的URL。
* /
公共类MainTest扩展的AsyncTask<字符串,整数,字符串> { @覆盖
在preExecute保护无效(){
pDialog =新ProgressDialog(MainActivity.this);
pDialog.setMessage(中..);
pDialog.setIndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
} @覆盖
保护字符串doInBackground(字符串... PARAMS){ 发布数据(); 返回null;
} @覆盖
保护无效onPostExecute(字符串结果){
// TODO自动生成方法存根 super.onPostExecute(结果);
//数据= jobj.toString();
pDialog.dismiss(); } }}
activity_main.xml中
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MainActivity。> < ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_marginTop =32dp
机器人:可点击=假
机器人:SRC =@绘制/图像/> <按钮
机器人:ID =@ + ID / SUBMIT_BUTTON_ID
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =47dp
机器人:文字=提交/>< / LinearLayout中>
于解决任何帮助这个!
数据= bos.toByteArray();
ByteArrayBody BAB =新ByteArrayBody(数据,forest.jpg);
reqEntity.addPart(钥匙,BAB);
}
赶上(例外五){
Log.v(MY-错误-标签,我得到了一个错误+ E);
reqEntity.addPart(钥匙,新StringBody());
}
postRequest.setEntity(reqEntity);
HTT presponse响应= httpClient.execute(postRequest);
的BufferedReader读者=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8)。);
串sResponse;
StringBuilder的S =新的StringBuilder();
而((sResponse = reader.readLine())!= NULL){
S = s.append(sResponse);
}
} 公共类MainTest扩展的AsyncTask<字符串,整数,字符串> { @覆盖
在preExecute保护无效(){
pDialog =新ProgressDialog(MainActivity.this);
pDialog.setMessage(中..);
pDialog.setIndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
} @覆盖
保护字符串doInBackground(字符串... PARAMS){ 尝试{
postImageData();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
} 返回null;
} @覆盖
保护无效onPostExecute(字符串结果){
// TODO自动生成方法存根 super.onPostExecute(结果);
//数据= jobj.toString();
pDialog.dismiss(); } }}
[FINAL-EDIT]
包com.example.datapostingproject;进口java.io.BufferedOutputStream;
进口java.io.BufferedReader中;
进口java.io.ByteArrayOutputStream中;
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.io.OutputStream中;
进口的java.util.ArrayList;
进口的java.util.List;
进口了java.util.Random;进口org.apache.http.Header;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.ParseException;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.entity.mime.HttpMultipartMode;
进口org.apache.http.entity.mime.MultipartEntity;
进口org.apache.http.entity.mime.content.ByteArrayBody;
进口org.apache.http.entity.mime.content.FileBody;
进口org.apache.http.entity.mime.content.StringBody;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.apache.http.util.EntityUtils;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.graphics.Bitmap;
进口android.graphics.Bitmap.Com pressFormat;
进口android.graphics.BitmapFactory;
进口android.graphics.drawable.BitmapDrawable;
进口android.graphics.drawable.Drawable;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.os.Environment;
进口android.util.Base64;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ImageView;公共类MainActivity延伸活动{ 按钮提交;
ProgressDialog pDialog;
InputStream为; ImageView的ImageView的; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 提交=(按钮)findViewById(R.id.SUBMIT_BUTTON_ID); ImageView的=(ImageView的)findViewById(R.id.imageView1); submit.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
新MainTest()执行();
}
});
} / **
*方法张贴图像到服务器。
* U将不得不改变,这将接受图像数据的URL。
*引发IOException
* /
公共无效postImageData(){
位图bitmapOrg = BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream宝=新ByteArrayOutputStream();
bitmapOrg.com preSS(Bitmap.Com pressFormat.JPEG,90,包);
字节[] BA = bao.toByteArray();
字符串BA1 = Base64.en codeToString(BA,Base64.DEFAULT);
ArrayList的<&的NameValuePair GT; namevaluepairs中新=
ArrayList的<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(钥匙,BA1));
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新
HttpPost(http://10.0.2.2:7002/Details/);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}赶上(例外五){
Log.e(log_tag,在HTTP连接错误+ e.toString());
}
}
公共类MainTest扩展的AsyncTask<字符串,整数,字符串> { @覆盖
在preExecute保护无效(){
pDialog =新ProgressDialog(MainActivity.this);
pDialog.setMessage(中..);
pDialog.setIndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
} @覆盖
保护字符串doInBackground(字符串... PARAMS){ postImageData(); 返回null;
} @覆盖
保护无效onPostExecute(字符串结果){
// TODO自动生成方法存根 super.onPostExecute(结果);
//数据= jobj.toString();
pDialog.dismiss(); } }}
{调试堆栈斯特斯} - 日志
14 12-08:47:50.628:I / .......(433):类型错误:未定义不能读取属性的钥匙
12-08 14:47:50.628:I / .......(433):在C:\\防爆pressPractice \\ imageUpload \\ app.js:17:32
12-08 14:47:50.628:I / .......(433):在回调(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\指数。 JS:164:37)
12-08 14:47:50.628:I / .......(433):在参数(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\指数。 JS:138:11)
12-08 14:47:50.628:I / .......(433):在通(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\指数。 JS:145:5)
12-08 14:47:50.628:I / .......(433):在Router._dispatch(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\ index.js:173:5)
12-08 14:47:50.628:I / .......(433):在Object.router(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\ index.js:33:10)
12-08 14:47:50.628:I / .......(433):在明年(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ node_modules \\连接\\ lib目录\\ proto.js:193:15)
12-08 14:47:50.628:I / .......(433):在多部分(C:\\Ex$p$pssPractice\\imageUpload\
ode_modules\\ex$p$pss\
ode_modules\\connect\\lib\\middleware\\multipart.js:86:27)
12-08 14:47:50.628:I / .......(433):在C:\\Ex$p$pssPractice\\imageUpload\
ode_modules\\ex$p$pss\
ode_modules\\connect\\lib\\middleware\\bodyParser.js:57:9
12-08 14:47:50.628:I / .......(433):在C:\\Ex$p$pssPractice\\imageUpload\
ode_modules\\ex$p$pss\
ode_modules\\connect\\lib\\middleware\\urlen$c$cd.js:76:7
12-08 14:47:50.639:E / log_tag(433):错误的HTTP连接java.lang.IllegalStateException:内容已被消耗
删除后的第二错误跟踪是= entity.getContent();
15 12-08:04:10.971:I / .......(461):类型错误:未定义不能读取属性的钥匙
12-08 15:04:10.971:I / .......(461):在C:\\防爆pressPractice \\ imageUpload \\ app.js:17:32
12-08 15:04:10.971:I / .......(461):在回调(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\指数。 JS:164:37)
12-08 15:04:10.971:I / .......(461):在参数(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\指数。 JS:138:11)
12-08 15:04:10.971:I / .......(461):在通(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\指数。 JS:145:5)
12-08 15:04:10.971:I / .......(461):在Router._dispatch(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\ index.js:173:5)
12-08 15:04:10.971:I / .......(461):在Object.router(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ lib目录\\路由器\\ index.js:33:10)
12-08 15:04:10.971:I / .......(461):在明年(C:\\防爆pressPractice \\ imageUpload \\ node_modules \\ EX preSS \\ node_modules \\连接\\ lib目录\\ proto.js:193:15)
12-08 15:04:10.971:I / .......(461):在多部分(C:\\Ex$p$pssPractice\\imageUpload\
ode_modules\\ex$p$pss\
ode_modules\\connect\\lib\\middleware\\multipart.js:86:27)
12-08 15:04:10.971:I / .......(461):在C:\\Ex$p$pssPractice\\imageUpload\
ode_modules\\ex$p$pss\
ode_modules\\connect\\lib\\middleware\\bodyParser.js:57:9
12-08 15:04:10.971:I / .......(461):在C:\\Ex$p$pssPractice\\imageUpload\
ode_modules\\ex$p$pss\
ode_modules\\connect\\lib\\middleware\\urlen$c$cd.js:76:7
但是,从邮差当我发送图像它为关键
在静止图像获取贴不成功!
解决方案
使用下面的一个尝试
公共无效POSTDATA(){
位图bitmapOrg = BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream宝=新ByteArrayOutputStream();
bitmapOrg.com preSS(Bitmap.Com pressFormat.JPEG,90,包);
字节[] BA = bao.toByteArray();
字符串BA1 = Base64.en codeToString(BA,Base64.DEFAULT);
ArrayList的<&的NameValuePair GT; namevaluepairs中新=
ArrayList的<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(形象,BA1));
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新
HttpPost(http://10.0.2.2:7002/Details/);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}赶上(例外五){
Log.e(log_tag,在HTTP连接错误+ e.toString());
}
}
编辑:
只是用于测试从尝试使用SD卡的文件路径上传图片
尝试
{
字符串的文件路径= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+\"Download\"+File.separator+\"ic_launcher.png\";
HttpClient的HttpClient的=新DefaultHttpClient();
httpclient.getParams()的setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1)。
HttpPost httppost =新HttpPost(你的URL);
档案文件=新的文件(文件路径);
MultipartEntity mpEntity =新MultipartEntity();
ContentBody cbFile =新FileBody(文件,图像/ JPEG);
mpEntity.addPart(userfile的cbFile);
httppost.setEntity(mpEntity);
的System.out.println(执行要求+ httppost.getRequestLine());
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
字符串_response = EntityUtils.toString(实体);
Log.i(......,_响应);
}赶上(例外五){
e.printStacktrace();
}
I am trying for a simple image upload to local server
What I am trying to do :: I am trying to upload one image to server on click of button
- I have referred this link
- But i don't know how to implement the
postData()
function
Link i am trying to post the image::
http://10.0.2.2/Details/
with a name key
for the image
MainActivity.java
public class MainActivity extends Activity {
Button submit;
ProgressDialog pDialog;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);
imageView = (ImageView) findViewById(R.id.imageView1);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();
}
});
}
public void postData() {
}
/**
* Method to post the image to the server.
* U will have to change the url which will accept the image data.
*/
public class MainTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
postData();
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:clickable="false"
android:src="@drawable/image" />
<Button
android:id="@+id/SUBMIT_BUTTON_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="47dp"
android:text="SUBMIT" />
</LinearLayout>
Any help on resolving this !
[EDIT- What i tried before]- I was not successful in getting the result
public class MainActivity extends Activity {
Button submit;
ProgressDialog pDialog;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);
imageView = (ImageView) findViewById(R.id.imageView1);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();
}
});
}
/**
* Method to post the image to the server.
* U will have to change the url which will accept the image data.
* @throws IOException
*/
public void postImageData() throws IOException
{
Drawable myDrawable = getResources().getDrawable(R.drawable.image);
Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
reqEntity.addPart("key", bab);
}
catch(Exception e){
Log.v("MY-Error-Tag", "I got an error"+e);
reqEntity.addPart("key", new StringBody(""));
}
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
}
public class MainTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
postImageData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
[FINAL-EDIT]
package com.example.datapostingproject;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button submit;
ProgressDialog pDialog;
InputStream is;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);
imageView = (ImageView) findViewById(R.id.imageView1);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();
}
});
}
/**
* Method to post the image to the server.
* U will have to change the url which will accept the image data.
* @throws IOException
*/
public void postImageData() {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("key",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://10.0.2.2:7002/Details/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
public class MainTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
postImageData();
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
{stack stace on debugging } - in log
12-08 14:47:50.628: I/.......(433): TypeError: Cannot read property 'key' of undefined
12-08 14:47:50.628: I/.......(433): at C:\ExpressPractice\imageUpload\app.js:17:32
12-08 14:47:50.628: I/.......(433): at callbacks (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:164:37)
12-08 14:47:50.628: I/.......(433): at param (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:138:11)
12-08 14:47:50.628: I/.......(433): at pass (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:145:5)
12-08 14:47:50.628: I/.......(433): at Router._dispatch (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:173:5)
12-08 14:47:50.628: I/.......(433): at Object.router (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:33:10)
12-08 14:47:50.628: I/.......(433): at next (C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\proto.js:193:15)
12-08 14:47:50.628: I/.......(433): at multipart (C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\middleware\multipart.js:86:27)
12-08 14:47:50.628: I/.......(433): at C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9
12-08 14:47:50.628: I/.......(433): at C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\middleware\urlencoded.js:76:7
12-08 14:47:50.639: E/log_tag(433): Error in http connection java.lang.IllegalStateException: Content has been consumed
second-error trace after removing is = entity.getContent();
12-08 15:04:10.971: I/.......(461): TypeError: Cannot read property 'key' of undefined
12-08 15:04:10.971: I/.......(461): at C:\ExpressPractice\imageUpload\app.js:17:32
12-08 15:04:10.971: I/.......(461): at callbacks (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:164:37)
12-08 15:04:10.971: I/.......(461): at param (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:138:11)
12-08 15:04:10.971: I/.......(461): at pass (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:145:5)
12-08 15:04:10.971: I/.......(461): at Router._dispatch (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:173:5)
12-08 15:04:10.971: I/.......(461): at Object.router (C:\ExpressPractice\imageUpload\node_modules\express\lib\router\index.js:33:10)
12-08 15:04:10.971: I/.......(461): at next (C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\proto.js:193:15)
12-08 15:04:10.971: I/.......(461): at multipart (C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\middleware\multipart.js:86:27)
12-08 15:04:10.971: I/.......(461): at C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9
12-08 15:04:10.971: I/.......(461): at C:\ExpressPractice\imageUpload\node_modules\express\node_modules\connect\lib\middleware\urlencoded.js:76:7
But from POSTMAN when i send image it works for 'key'
Still not successful in getting image posted !
解决方案
Use the below an try
public void postData() {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://10.0.2.2:7002/Details/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
Edit:
Just for testing try uploading image from sdcard using file path
try
{
String filepath= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Download"+File.separator+"ic_launcher.png";
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("your url");
File file = new File(filepath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String _response=EntityUtils.toString(entity);
Log.i(".......",_response);
}catch(Exception e){
e.printStacktrace();
}
这篇关于张贴到服务器的单个图像局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文