freedom_wings4 2016-09-17 06:50 采纳率: 0%
浏览 1308

安卓上传图片到asp.net mvc服务器,服务端接收不到,求解决方法

安卓客户端,与上传图片有关的代码

  String BOUNDARY = UUID.randomUUID().toString();
    String PREFIX = "--";

         try {
                        ByteArrayOutputStream bitmapByteOutput=new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bitmapByteOutput);     //bitmap是要上传的图片
                        String beginLine=BOUNDARY+"\r\n";
                        String param="Content-Disposition=from-data&name=imageFile&fileName=imageFile&Content-Type=image/jpeg\r\n";
                       String endLine=BOUNDARY+PREFIX+"\r\n";

                        byte[] bitmapByte=bitmapByteOutput.toByteArray();
                        URL url = new URL("http://192.168.1.102:63116/UploadTest/UploadImage");
                        HttpURLConnection con=(HttpURLConnection)url.openConnection();
                        con.setUseCaches(false);
                        con.setDoOutput(true);
                        con.setRequestMethod("POST");
                        con.setRequestProperty("Content-Type", "multipart/from-data");
                        con.setRequestProperty("Content-Length",String.valueOf( beginLine.getBytes().length+param.getBytes().length+bitmapByte.length+endLine.getBytes().length));
                        OutputStream out=con.getOutputStream();
                        out.write(beginLine.getBytes());
                        out.write(param.getBytes());
                        out.write(bitmapByte);
                        out.write(endLine.getBytes());
                        out.flush();

                        if(con.getResponseCode()==200)
                        {
                            InputStream input=con.getInputStream();
                            ByteArrayOutputStream output=new ByteArrayOutputStream();
                            int b;
                            while((b=input.read())!=-1)
                                output.write(b);
                            String response=output.toString();
                            input.close();
                            output.close();
                            con.disconnect();

                           }

                    }
                    catch(Exception e)
                    {
                        Log.e("this","上传图片的时候出错了"+e);
                    }

我的ASP.NET MVC服务端与接收图片有关的代码,是controller中的一个action

 public ActionResult uploadImage(HttpPostedFileBase imageFile)
        {
            try
            {

                string name = imageFile.FileName;
                string fileName = Path.GetFileName(name);
                string filePath = Server.MapPath("/ImageTest/" + fileName);
                imageFile.SaveAs(filePath);
                return Content("success");
            }
            catch (Exception ex)
            {
                return Content("fail"+ex);
            }
        }

现在客户端接收到服务端返回错误的信息(上面asp.net mvc若出错了会把出错信息ex返回给客户端),就是NullReferenceException,未将对象实例设置到对象引用。指的应该是传进来的参数HttpPostedFileBase对象为null,那么这个是怎么解决呢?

应该是是这段安卓上传图片的代码有问题,但总是找不到问题所在,求指点

  • 写回答

1条回答

  • codelady 2016-09-17 16:14
    关注

    如果你前面请求连接成功的话,在output.write(b)后面也要调用write.flush()吧

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条