Java 如何将字节数组从Android设备发送到servlet

Java 如何将字节数组从Android设备发送到servlet,java,android,servlets,Java,Android,Servlets,我需要从android设备向Servlet发送一些字节数组。为此,我尝试使用下一个代码: Servlet: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DataInputStream in = new DataInputStream((InputStream)request.getInputStr

我需要从android设备向Servlet发送一些字节数组。为此,我尝试使用下一个代码:

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {

  DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
   response.setContentType("text/plain");
    byte[] buffer = new byte[1024];
    int len = 0;
    File file;
    file=new File(getServletContext().getRealPath("/POST_LOG!!!!.txt"));
    if(!file.exists()){
        file.createNewFile();
    }
    while ((len = in.read(buffer)) > 0) {
          FileOutputStream fos = new FileOutputStream(getServletContext().getRealPath("/POST_LOG!!!!.txt"), true);

           fos.write(buffer);            
           fos.close(); 
    }

    PrintWriter out = response.getWriter();
    out.write("Done");
    out.close();
HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new InputStreamEntity(new MyInputStream(),
            4096 * 1024 * 10));
    HttpResponse response = null;

    try {
        response = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        httpPost.abort();
    } catch (IOException e) {
        e.printStackTrace();
        httpPost.abort();
    }
设备端:

URL uploadUrl;
    try {
        uploadUrl = new URL(url);
        HttpURLConnection c = (HttpURLConnection) uploadUrl
                .openConnection();
        c.setRequestMethod("POST");

        c.setDoInput(true);
        c.setDoOutput(true);
        c.setUseCaches(false);
        c.connect();
        OutputStream out = c.getOutputStream();

        for (int i = 0; i < 1000; i++) { // generate random bytes for
                                            // uploading
            byte[] buffer = new byte[256];
            for (int j = 0; j < 256; j++) {
                Random r = new Random();
                buffer[j] = (byte) r.nextInt();
            }

            out.write(buffer);
            out.flush();
        }

        out.close();

    } catch (Exception e) {
        MessageBox("Error. " + e.toString());
    }

    return (long) 0;
}
URL上传URL;
试一试{
uploadUrl=新URL(URL);
HttpURLConnection c=(HttpURLConnection)上传URL
.openConnection();
c、 setRequestMethod(“POST”);
c、 setDoInput(true);
c、 设置输出(真);
c、 setUseCaches(假);
c、 connect();
OutputStream out=c.getOutputStream();
对于(int i=0;i<1000;i++){//为生成随机字节
//上传
字节[]缓冲区=新字节[256];
对于(int j=0;j<256;j++){
随机r=新随机();
缓冲区[j]=(字节)r.nextInt();
}
输出。写入(缓冲区);
out.flush();
}
out.close();
}捕获(例外e){
MessageBox(“Error.+e.toString());
}
返回(长)0;
}

我不明白为什么这个代码不起作用。当我尝试调试POST方法时,它甚至没有被调用。我将感谢您的示例

您有很多选择:

  • 发送字节值:125,11,25,40(这是一个哑选项)
  • 发送base64或十六进制编码,然后解码(使用apache commons编解码器)
  • 以多部分/表单数据的形式提交

    • 我找到了解决办法。我刚刚使用自定义InputStream更改了设备端代码

      设备端:

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
      ServletException, IOException {
      
        DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
         response.setContentType("text/plain");
          byte[] buffer = new byte[1024];
          int len = 0;
          File file;
          file=new File(getServletContext().getRealPath("/POST_LOG!!!!.txt"));
          if(!file.exists()){
              file.createNewFile();
          }
          while ((len = in.read(buffer)) > 0) {
                FileOutputStream fos = new FileOutputStream(getServletContext().getRealPath("/POST_LOG!!!!.txt"), true);
      
                 fos.write(buffer);            
                 fos.close(); 
          }
      
          PrintWriter out = response.getWriter();
          out.write("Done");
          out.close();
      
      HttpPost httpPost = new HttpPost(url);
          httpPost.setEntity(new InputStreamEntity(new MyInputStream(),
                  4096 * 1024 * 10));
          HttpResponse response = null;
      
          try {
              response = httpClient.execute(httpPost);
          } catch (ClientProtocolException e) {
              e.printStackTrace();
              httpPost.abort();
          } catch (IOException e) {
              e.printStackTrace();
              httpPost.abort();
          }
      

      请共享您的web.xml servlet配置条目、URL映射以及用于调用它的servletn URL。