Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HttpPost httppost = new HttpPost("some path");
- HttpClient httpclient = new DefaultHttpClient();
- try {
- File file = new File("file path");
- InputStream in = new BufferedInputStream(new FileInputStream(file));
- byte[] bArray = new byte[(int) file.length()];
- in.read(bArray);
- String entity = Base64.encodeToString(bArray, Base64.DEFAULT);
- httppost.setEntity(new StringEntity(entity));
- HttpResponse response = httpclient.execute(httppost);
- }
- private class FileUploadTask extends AsyncTask<Object, Integer, Void> {
- private ProgressDialog dialog;
- @Override
- protected void onPreExecute() {
- dialog = new ProgressDialog(TheActivity.this);
- dialog.setMessage("Uploading...");
- dialog.setIndeterminate(false);
- dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- dialog.setProgress(0);
- dialog.show();
- }
- @Override
- protected Void doInBackground(Object... arg0) {
- try {
- File file = new File("file path");
- FileInputStream fileInputStream = new FileInputStream(file);
- byte[] bytes = new byte[(int) file.length()];
- fileInputStream.read(bytes);
- fileInputStream.close();
- URL url = new URL("some path");
- HttpURLConnection connection =
- (HttpURLConnection) url.openConnection();
- OutputStream outputStream = connection.getOutputStream();
- int bufferLength = 1024;
- for (int i = 0; i < bytes.length; i += bufferLength) {
- int progress = (int)((i / (float) bytes.length) * 100);
- publishProgress(progress);
- if (bytes.length - i >= bufferLength) {
- outputStream.write(bytes, i, bufferLength);
- } else {
- outputStream.write(bytes, i, bytes.length - i);
- }
- }
- publishProgress(100);
- outputStream.close();
- outputStream.flush();
- InputStream inputStream = connection.getInputStream();
- // read the response
- inputStream.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Integer... progress) {
- dialog.setProgress(progress[0]);
- }
- @Override
- protected void onPostExecute(Void result) {
- try {
- dialog.dismiss();
- } catch(Exception e) {
- }
- }
- }
Add Comment
Please, Sign In to add comment