从android上传图像servlet......这段代码有什么问题
这是 android 代码
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();
}
这两个代码都不会生成错误,但是当我运行它们时,我在服务器上看不到图像。谁能帮我解决这个问题吗?
Here is the android code
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());
}
}
}
Heres my Servlet code....
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();
}
Both these codes do not generate an error, but when I run them I dont see the image on the server. Can anyone please help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在将图像分配给字符串之前创建名称值对:您需要在将图像分配给字符串之后创建名称值对。这就是服务器找不到该文件的原因。
更改:
至
You are creating the name value pair before you assign the image to the String: you need to create the name value pair AFTER you assign the image to the String. This is why the server doesn't find the file.
Change:
to