0
下面是Android代碼圖像的servlet上傳.....哪些錯誤與此代碼
public class upload extends Activity {
InputStream is;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//Bitmap bitmapOrg = BitmapFact0ory.decodeResource(getResources(),
//R.drawable.a1);
Bitmap bitmapOrg = BitmapFactory.decodeFile("/sdcard/pradeep.jpg");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
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:8080/upload/uploadedimg");
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());
}
}
}
我的繼承人servlet代碼....
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
InputStream in = request.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String line;
//Read the BufferedReader out and receives String data
while ((line = r.readLine())!=null) {
buf.append(line);
}
String imageString = buf.toString();
byte[] imageByteArray = Base64.decode(imageString);
FileOutputStream f = new FileOutputStream("C:/test.jpg");
f.write(imageByteArray);
f.close();
}
這兩種代碼不能生成一個錯誤,但是當我運行它們時,我沒有看到服務器上的圖像。任何人都可以幫助我嗎?
@Femi ...我已經改變了我的代碼如下.. – prade 2011-05-08 07:19:52