从android上传图像servlet......这段代码有什么问题

发布于 2024-11-05 09:34:29 字数 2356 浏览 1 评论 0原文

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-11-12 09:34:29

您在将图像分配给字符串之前创建名称值对:您需要在将图像分配给字符串之后创建名称值对。这就是服务器找不到该文件的原因。

更改:

String ba1;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    try
    {
         ba1=Base64.encodeBytes(ba);

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
     nameValuePairs.add(new BasicNameValuePair("image",Base64.encodeBytes(ba)));

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:

String ba1;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    try
    {
         ba1=Base64.encodeBytes(ba);

to

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
     nameValuePairs.add(new BasicNameValuePair("image",Base64.encodeBytes(ba)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文