Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. public class MultipartSender extends AsyncTask<Void, Void, Void>
  2.      {
  3.      DataOutputStream dos;
  4.      String lineEnd = "\r\n";
  5.      String boundary =  "*****";
  6.      int bufferSize = 5*1024*1024;
  7.      long fileSize;
  8.      private void writeFilePart(byte[] buffer,int bytesRead,long fragmentCount) throws IOException
  9.          {
  10.          dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"13.3gp("+ fragmentCount +")\"" + lineEnd);
  11.          Log.d(O.TAG,"writeFilePart: фрагмент "+ fragmentCount +" записан");
  12.          dos.writeBytes(lineEnd);
  13.          dos.write(buffer, 0, bytesRead);
  14.          dos.writeBytes(lineEnd);
  15.          dos.writeBytes("--" + boundary + "--" + lineEnd);
  16.          }
  17.  
  18.      @Override
  19.      protected Void doInBackground(Void... arg)
  20.          {
  21.          Log.d(O.TAG,"doInBackground: поехали");
  22.          HttpURLConnection conn;
  23.          File file= new File("/storage/sdcard1/DCIM/Camera/13.rar");
  24.          Log.d(O.TAG,"doInBackground: file.exist: "+ file.exists() );
  25.          Log.d(O.TAG,"doInBackground: "+ file);
  26.          if( !file.exists() )
  27.              return null;
  28.          int bytesRead;
  29.          byte[] buffer;
  30.          fileSize= file.length();
  31.          String urlString = %ссылка%;
  32.          try
  33.              {
  34.              FileInputStream fileInputStream = new FileInputStream(file);
  35.              URL url = new URL(urlString);
  36.              conn = (HttpURLConnection) url.openConnection();
  37.              conn.setDoInput(true);
  38.              conn.setDoOutput(true);
  39.              conn.setUseCaches(false);
  40.              conn.setRequestMethod("POST");
  41.              conn.setRequestProperty("Connection", "Keep-Alive");
  42.              conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  43.              dos = new DataOutputStream( conn.getOutputStream() );
  44.              dos.writeBytes("--" + boundary + lineEnd);
  45.              buffer = new byte[bufferSize];
  46.              long writenFragments=0,progress;
  47.              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  48.              while (bytesRead > 0)
  49.                  {
  50.                  writenFragments++;
  51.                  writeFilePart(buffer,bytesRead,writenFragments);
  52.                  progress= writenFragments*bufferSize/fileSize;
  53.                  Log.d(O.TAG,"doInBackground: progress: "+ progress +"%");
  54.                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  55.                  }
  56.              dos.writeBytes(lineEnd);
  57.              dos.writeBytes("--" + boundary + "--" + lineEnd);
  58.              fileInputStream.close();
  59.              dos.flush();
  60.              dos.close();
  61.              }
  62.          catch (IOException ioe)
  63.              {
  64.               return null;
  65.              }
  66.          try
  67.              {
  68.              Log.d("c123","doInBackground: "+conn.getResponseCode());
  69.              }
  70.          catch (IOException ioex)
  71.              {
  72.              Log.e("Debug", "error: " + ioex.getMessage(), ioex);
  73.              }
  74.          return null;
  75.          }
  76.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement