jax-rs detect aborting upload

php_gutu

Mitglied
Hallo zusammen

Ich habe folgenden Wunsch: In der Rest Schnittstelle sollte ich darauf reagieren können, wenn der Client den Upload Prozess abbricht, sei das selber von ihm veranlasst, oder weil seine Internetverbindung unterbrochen wurde. Wie könnte ich das umsetzen? Ist so etwas möglich? In z.B. PHP gibt es z.B. die Funktion int connection_aborted(void). Ich mag aber PHP nicht besonders gerne :)

Hat jemand einen Tipp.
Anbei der Code (ich halte mich gerne am Java EE Standard und nutze keine spezifische Server Implementierungen für den File upload)

Java:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response upload(@PathParam("id") String id, @Context final HttpServletRequest request) {
  // check auth.
  if (this.jwtUserData == null) {
    return Response.status(401).build();
  }

  // save file
  File pdfUploadDestination = new File("/path/to/upload/" + id + "/fileName.pdf");
  try (final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream(), 8192)) {
    try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(pdfUploadDestination))) {
      byte[] buffer = new byte[8192];
      int length;
      while ((length = inputStream.read(buffer)) != -1) {
        stream.write(buffer, 0, length);
      }
       // close stream manually, why i will rename file after pdf validation.
      stream.close();
    }

    // validate file
    PdfValidator.validate(pdfUploadDestination);
    // ..rename file

    // response: upload was successfull.
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("message", "successfull upload");
    return Response.status(200).entity(jsonObject.toString()).build();
  } catch (FileNotFoundException e) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("error", "the uploaded file doesn't exist.");
    return Response.status(400).entity(jsonObject.toString()).build();
  } catch (IOException e) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("error", "io exception");
    return Response.status(400).entity(jsonObject.toString()).build();
  }
}
 

Flown

Administrator
Mitarbeiter
Du kannst auf deinen InputStream (eigtl. ServletInputStream) einen ReadListener dranhängen und mal überprüfen, ob auf onError etwas kommt.
 

Ähnliche Java Themen

Neue Themen


Oben