简单的http服务器,用于通过wifi

时间:2016-07-21 13:33:19

标签: java android http

我正在开发应用程序,它将从一个Android设备发送一些文件到另一个设备。一个设备将创建一个热点,并将使用Web浏览器从另一个设备接收一些文件,这些文件将使用WiFi连接到热点。我对HTTP知之甚少。我在发送设备中创建了简单的服务器。接收设备将通过其Web浏览器连接到服务器。当我在浏览器中写入(IP:端口号)时,它说“开始下载”但下载没有完成。在发送设备中我得到IOException:sendto失败的EPIPE(断管)。 我的代码是

boolean sending_done = false;
while (! sending_done)
{
    Socket socket = null;
    try
    {
       socket = serverSocket.accept();
       OutputStream outputStream = socket.getOutputStream();
       outputStream.write("HTTP/1.1 200 OK\r\n".getBytes());
       outputStream.write("Content-Type: application/zip\r\n".getBytes());
       outputStream.write("Content-Disposition: attachment; filename=files.zip\r\n".getBytes());
       outputStream.write("Content-Encoding: gzip\r\n".getBytes());
       outputStream.write("Transfer-Encoding: chunked\r\n".getBytes());
       outputStream.write("\r\n".getBytes());
       BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
       ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);
       zipOutputStream.setLevel(ZipOutputStream.STORED);
       sendFiles(zipOutputStream, to_send_files_array_list);

       byte[] CRLF ="\r\n".getBytes();
       writ_header(0, zipOutputStream, CRLF);
       zipOutputStream.write(CRLF, 0, CRLF.length);

       zipOutputStream.finish();
       zipOutputStream.flush();
       zipOutputStream.close();

       sending_done = true;

    }
    catch (IOException e)
   {
       e.printStackTrace();
       display_toast(e.toString());
   }
   finally
   {
       try
       {
          if (socket != null)
          {
              socket.close();
          }
       } catch (IOException e) {
          e.printStackTrace();
       }
   }
}


   private void sendFiles(ZipOutputStream zipOutputStream, ArrayList<File> filesToSend)
   {
      byte[] CRLF ="\r\n".getBytes();
      for(File file: filesToSend)
      {

          BufferedInputStream bufferedInputStream = null;
          ZipEntry zipEntry = null;

         try
         {
              FileInputStream fileInputStream = new FileInputStream(file);
              bufferedInputStream = new BufferedInputStream(fileInputStream);

              zipEntry = new ZipEntry(file.getName());
              zipOutputStream.putNextEntry(zipEntry);

              int x;
              byte[] buffer = new byte[buffer_size];
              while ((x= bufferedInputStream.read(buffer))!= -1)
              {
                  if (x>0)
                  {
                     writ_header(x, zipOutputStream, CRLF);
                     zipOutputStream.write(buffer,0,x);
                     zipOutputStream.write(CRLF, 0, CRLF.length);
                   }
              }
         }
        catch (IOException e)
       {
           e.printStackTrace();
       }
       finally
       {
           try
           {
              if (zipEntry != null)
              {
                 zipOutputStream.closeEntry();
              }
              if (bufferedInputStream != null)
              {
                  bufferedInputStream.close();
              }
           }
           catch (Exception e)
           {
              e.printStackTrace();
           }

       }
    }
 }

 private void  writ_header(int length, OutputStream outputStream, byte[] CRLF) throws IOException
{
    byte[] header = Integer.toHexString(length).getBytes();
    outputStream.write(header, 0, header.length);
    outputStream.write(CRLF, 0, CRLF.length);
}

0 个答案:

没有答案