2 baidu 28895431 baidu_28895431 于 2015.06.09 20:17 提问

安卓与Servlet交互,空指针

客户端关键源码如下:
AsyncHttpClient add=new AsyncHttpClient();
add.addHeader("charset", HTTP.UTF_8);
add.addHeader("Content-Type", "text/plain");
price=pri.getText().toString();
RequestParams params=new RequestParams();
RequestParams picparms=new RequestParams();

        //截取图片名称
        String upic=realPath.substring(realPath.lastIndexOf("/")+1, realPath.length());
        File pic=new File(realPath);
        System.out.println(upic);
        params.add("type", "mobile");
        params.add("model", model);
        params.add("brand", brand);
        params.add("price", "1200");
        params.add("picPath", upic);
        //图片上传

        try {
            picparms.put("pic", new PhotoUpload().Upload(realPath));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            System.out.println(">>>>>>>>>>图片大小为:"+pic.length());

        System.out.println("基本信息:"+params.toString());
        add.get(URL, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                // TODO Auto-generated method stub
                System.out.println("服务器连接成功");
                if(arg0==200)
                {
                    boolean b = Boolean.valueOf(new String(arg2));
                    if(b)
                    {
                        Toast.makeText(UpdateCom.this, "图片上传成功", Toast.LENGTH_SHORT).show();
                    }else
                    {
                        Toast.makeText(UpdateCom.this, "图片上传失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                // TODO Auto-generated method stub
                System.out.println("服务器连接失败");
            }
        });
        add.post(URL, picparms, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                // TODO Auto-generated method stub
                System.out.println("服务器连接成功");
                if(arg0==200)
                {
                    boolean b = Boolean.valueOf(new String(arg2));
                    if(b)
                    {
                        Toast.makeText(UpdateCom.this, "基本信息上传成功", Toast.LENGTH_SHORT).show();
                    }else
                    {
                        Toast.makeText(UpdateCom.this, "基本信息上传失败", Toast.LENGTH_SHORT).show();
                    }

                }
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                // TODO Auto-generated method stub
                System.out.println("服务器连接失败");
            }
            //显示上传进度
            public void onProgress(int bytesWritten, int totalSize) {  
                    // TODO Auto-generated method stub  
                    super.onProgress(bytesWritten, totalSize);  
                    int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);  
                    // 上传进度显示  
                    progress.setProgress(count);  
                    Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);  
                }  
        });

        break;
    default:
        break;
    }
}
为什么有post和get呢,就是楼主要问的,应为post传上去,更本就拿不出来,而get不能传图片等大文件;所以就有了两个parms,对了,我在这里用到了AsyncHttpClient;

服务器关键代码为:
package package_for_ordinary_person;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import com.beans.CompetGoods;
import com.dao_for_interface.CompGoodsDao;

/**

  • Servlet implementation class AddComproduct
    */
    @WebServlet("/AddComproduct")
    public class AddComproduct extends HttpServlet {

    boolean flag;
    private static final long serialVersionUID = 1L;
    CompGoodsDao cgd = new CompGoodsDao();
    CompetGoods cgoods;
    java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
    public String brand,model,picPath;
    /**

    • @see HttpServlet#HttpServlet() */ public AddComproduct() { super(); // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    •  response)
      

      */
      @SuppressWarnings("resource")
      protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub

      String type = request.getParameter("type");
      brand = request.getParameter("brand");
      model = request.getParameter("model");
      String price = request.getParameter("price");
      picPath = request.getParameter("picPath");
      System.out.println(type + brand + model + price+picPath);
      cgoods = new CompetGoods();
      cgoods.setType(type);
      cgoods.setBrand(brand);
      cgoods.setModel(model);
      cgoods.setCprice(price);
      cgoods.setUploadDate(currentDate);
      if (cgd.upload(cgoods)) {
      flag = true;
      } else {
      flag = false;
      }

      response.setContentType("text/plain,charset=UTF-8");
      PrintWriter out = response.getWriter();
      out.print(flag);
      }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    •  response)
      

      */
      protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      cgoods=new CompetGoods();
      System.out.println(">>>>>" + request.getContentLength());
      InputStream is = request.getInputStream();
      // String picdate=request.getParameter("pic");
      // System.out.println(">>>>>" + picdate.length());
      File file=new File("//usr//local//ImageFromClient//"+picPath);
      if (!file.exists()) {
      try {
      System.out.println("文件不存在,正在创建.路径为" + file);
      file.createNewFile();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      FileOutputStream fos1 = new FileOutputStream(file);
      // fos1.write(new sun.misc.BASE64Decoder().decodeBuffer(is));
      int i = 0;
      while ((i = is.read()) != -1) {
      fos1.write(i);
      }
      fos1.close();
      is.close();

      // 图片保存并入库
      /*FileOutputStream fos = null;
      File file = new File("//usr//local//ImageFromClient//" + picPath);
      if (!file.exists()) {
      try {
      System.out.println("文件不存在,正在创建.路径为" + file);
      file.createNewFile();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      fos = new FileOutputStream(file);*/
      /*

      • try { //写入的是字节流 //通过解码之后直接生成图片 //picPath存储的是图片文件字节 fos.write(new
      • sun.misc.BASE64Decoder().decodeBuffer(picPath));
      • System.out.println("成功写入" + file); } catch (IOException e) { // TODO
      • Auto-generated catch block e.printStackTrace(); } */ cgoods.setBrand(brand); cgoods.setModel(model); cgoods.setCpicPath(file.toString()); if (cgd.updatecom(cgoods)) { flag = true; } else { flag = false; }

      response.setContentType("text/plain,charset=UTF-8");
      PrintWriter out = response.getWriter();
      out.print(flag);
      }

}
另外补充,post方法的request是可以打印出来大小的,就是取不出来.

1个回答

xby1993
xby1993   2015.06.09 23:55

异常栈怎么没贴出来?

Csdn user default icon
上传中...
上传图片
插入图片