Search code examples
springamazon-web-servicesandroid-studiomodel-view-controller

Sending a file from Android device to AWS S3 through server spring boot application


I want to send a mp4 file from android device to AWS S3 storage bucket through spring boot application on heroku server. Which method I should use on my android app side to send the file?

I got following code on server side:

@RequestMapping("/add")
@Controller
public class AddProductController {

   @Autowired
   private FileService fileService;

   @GetMapping
   public ModelAndView add (){
      return getFormMAV(new Product());
   }

   @PostMapping
    public ModelAndView add(@RequestParam("file") MultipartFile file, @Valid Product product, BindingResult bindingResult){
        LOG.info("File received {}", file);
        LOG.info("Product received {}", product);

        if (bindingResult.hasErrors()){
            if (file.isEmpty()){
                bindingResult.addError(new FieldError("product", "imagePath", "Plik nie moze byc pusty!"));
            }
            return getFormMAV(product);
        } else {
            try {
                String uploadedFile = fileService.store(file);
                LOG.info("File received {}", uploadedFile);
                product.setImagePath(uploadedFile);
                //product.setTimestamp(LocalDateTime.now());
                productService.save(product);

                return new ModelAndView ("redirect:/");

            } catch (IOException | FileServiceException e) {
                e.printStackTrace();
                LOG.error("Error during file store", e);

                bindingResult.addError(new FieldError("product",
                        "imagePath",
                        "Problem z wysylka pliku"));

                return getFormMAV(product);
            }
        }
    }
}

Solution

  • Temporarily solved (on the android app side):

            if (views.get("shareVideo").equals("true")){
                views.put("shareVideo", "false");
                System.out.println("sharing...");
                String url = "http://10.0.2.2:9089/add/";
                AsyncHttpClient client = new AsyncHttpClient();
                File file = new File(play.arr.androidLauncher.outputDirectory, "1.jpg");
                RequestParams params = new RequestParams();
                try {
                    params.put("file", file);
                } catch (FileNotFoundException e) {
                    //TODO: Handle error
                    e.printStackTrace();
                }
                //TODO: Reaming body with id "property". prepareJson converts property class to Json string. Replace this with with your own method
                client.post(play.arr.androidLauncher.getContext(), url, params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        System.out.print("Failed..");
                    }
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        System.out.print("Success..");
                    }
                    @Override
                    public boolean getUseSynchronousMode() {
                        return false;
                    }
    
                    @Override
                    public void setUseSynchronousMode(boolean useSynchronousMode) {
                    }
                });
            }