简体   繁体   中英

Java REST - call method after POST to REST

I'm using a Java REST service for a file upload. The file should land on my server, which it does, then move to Amazon S3 bucket.

The upload to the server is fine, but the 2nd call to another method does not work. I assume because there is a timeout issue?

The code to move the file to amazon works in another app, but I am not able to get it working within my REST project.

Here is the method:

     @POST
     @Path("/upload")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(@FormDataParam("file") InputStream inputStream,
               @FormDataParam("file") FormDataContentDisposition file,    @FormDataParam("filename") String filename){

    Logger log = Logger.getLogger("Mike");


    String response = "";
    File f = null;
      try {
       final String FILE_DESTINATION = "C://uploads//" + file.getFileName();
       f = new File(FILE_DESTINATION);
       OutputStream outputStream = new FileOutputStream(f);
       int size = 0;
       byte[] bytes = new byte[1024];
       while ((size = inputStream.read(bytes)) != -1) {
        outputStream.write(bytes, 0, size);
       }
       outputStream.flush();
       outputStream.close();

       log.info("upload complete for initial file!");
       //move file to Amazon S3 Bucket.
       AmazonS3 s3 = new AmazonS3Client(
                new ClasspathPropertiesFileCredentialsProvider());

       log.info("trying put request");
       PutObjectRequest request = new PutObjectRequest("site.address.org","/pdf/PDF_Web_Service/work/"+f.getName(),f);
        log.info(f.getName());
        log.info(f.getAbsolutePath());

        s3.putObject(request);
        log.info("put request complete");

       response = "File uploaded " + FILE_DESTINATION;
      } catch (Exception e) {
       e.printStackTrace();
      }




      return Response.status(200).entity(response).build();
     }

Specifically, here is the part not working. I am not getting any log info either:

//move file to Amazon S3 Bucket.
  AmazonS3 s3 = new AmazonS3Client( new ClasspathPropertiesFileCredentialsProvider()); log.info("trying put request"); PutObjectRequest request = new PutObjectRequest("site.address.org","/pdf/PDF_Web_Service/work/"+f.getName(),f); log.info(f.getName()); log.info(f.getAbsolutePath()); s3.putObject(request); log.info("put request complete"); 

Michael,

If it's a time-out issue, it's common practice to use guava's Listenable Future to chain your tasks together. What your web sequence will look like then is:

a) Client sends file

b) Server responds with 200 once file completes uploading.

c) Once the server is done loading the file, chain the future to then upload to S3.

Chaining listenable futures is common practice to separate functionality and ensure a time out doesn't occur by breaking up your code and essentially pipe-lining it.

Please let me know if you have any questions!

我将亚马逊代码移动到try块中,现在它可以工作了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM