Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MultipartSender extends AsyncTask<Void, Void, Void>
- {
- DataOutputStream dos;
- String lineEnd = "\r\n";
- String boundary = "*****";
- int bufferSize = 5*1024*1024;
- long fileSize;
- private void writeFilePart(byte[] buffer,int bytesRead,long fragmentCount) throws IOException
- {
- dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"13.3gp("+ fragmentCount +")\"" + lineEnd);
- Log.d(O.TAG,"writeFilePart: фрагмент "+ fragmentCount +" записан");
- dos.writeBytes(lineEnd);
- dos.write(buffer, 0, bytesRead);
- dos.writeBytes(lineEnd);
- dos.writeBytes("--" + boundary + "--" + lineEnd);
- }
- @Override
- protected Void doInBackground(Void... arg)
- {
- Log.d(O.TAG,"doInBackground: поехали");
- HttpURLConnection conn;
- File file= new File("/storage/sdcard1/DCIM/Camera/13.rar");
- Log.d(O.TAG,"doInBackground: file.exist: "+ file.exists() );
- Log.d(O.TAG,"doInBackground: "+ file);
- if( !file.exists() )
- return null;
- int bytesRead;
- byte[] buffer;
- fileSize= file.length();
- String urlString = %ссылка%;
- try
- {
- FileInputStream fileInputStream = new FileInputStream(file);
- URL url = new URL(urlString);
- conn = (HttpURLConnection) url.openConnection();
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setUseCaches(false);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
- dos = new DataOutputStream( conn.getOutputStream() );
- dos.writeBytes("--" + boundary + lineEnd);
- buffer = new byte[bufferSize];
- long writenFragments=0,progress;
- bytesRead = fileInputStream.read(buffer, 0, bufferSize);
- while (bytesRead > 0)
- {
- writenFragments++;
- writeFilePart(buffer,bytesRead,writenFragments);
- progress= writenFragments*bufferSize/fileSize;
- Log.d(O.TAG,"doInBackground: progress: "+ progress +"%");
- bytesRead = fileInputStream.read(buffer, 0, bufferSize);
- }
- dos.writeBytes(lineEnd);
- dos.writeBytes("--" + boundary + "--" + lineEnd);
- fileInputStream.close();
- dos.flush();
- dos.close();
- }
- catch (IOException ioe)
- {
- return null;
- }
- try
- {
- Log.d("c123","doInBackground: "+conn.getResponseCode());
- }
- catch (IOException ioex)
- {
- Log.e("Debug", "error: " + ioex.getMessage(), ioex);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement