Skip to content

Instantly share code, notes, and snippets.

@smigniot
Created July 27, 2018 09:15
Show Gist options
  • Save smigniot/420ed499e549799c1133eae28c455ea6 to your computer and use it in GitHub Desktop.
Save smigniot/420ed499e549799c1133eae28c455ea6 to your computer and use it in GitHub Desktop.
File serving jaxrs test
package com.example.jaxrs;
import org.apache.commons.io.IOUtils;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.io.*;
import java.util.logging.Logger;
import static javax.ws.rs.core.MediaType.*;
public class ServeFiles {
// @POST // Correction #2
@GET // How can you POST a form, in json format and expect a redirect ?
@Path("/{loginId}")
// @Produces(MULTIPART_FORM_DATA) // Correction #1
@Produces(APPLICATION_OCTET_STREAM) // Produce bytes only
@Consumes(APPLICATION_JSON)
// public Response downloadExportedFile(@PathParam("loginId") String loginId, ExportFileDTO fileDetails) throws IOException {
public Response downloadExportedFile(@PathParam("loginId") String loginId) throws IOException {
Logger.getAnonymousLogger().info("Request");
File exportFolder = new File("C://directory");
File[] listOfFiles = exportFolder.listFiles();
for (File listOfFile : listOfFiles) {
//if (listOfFile.getName().equals(fileDetails.getFileName())) {
// Commented for now => it serves the first file
InputStream is = new FileInputStream(listOfFile.getAbsolutePath());
byte[] buffer = IOUtils.toByteArray(is);
Logger.getAnonymousLogger().info("Serving !");
return Response.ok(listOfFile)
.header("content-disposition", "attachment; filename=" + new File(listOfFile.getName()).getName())
.type(APPLICATION_OCTET_STREAM_TYPE).build();
//}
}
Logger.getAnonymousLogger().info("Failure");
return Response.serverError().build();
}
}
<html><body>
<script type="text/javascript">
//
// See https://stackoverflow.com/questions/9718102/how-does-jquery-post-deal-with-content-disposition-attachment#answer-9718122
//
// 1. If it's a GET, it is downloaded
// 2. If it's a POST, it has to be a standard form,
// with enctype='multipart/form-data'
// or with enctype='application/x-www-form-urlencoded'
// 3. Else :
//
// There is simply no way to have
// * a FORM
// * perform a POST
// * in Content-Type: application/json
// That doesn't exist
//
// So the client side (React as you said) IS makeing the call
// And as it's a _programmatic_ call the user does not get
// prompted to save the file
//
// You will have to let us know more of your
// client-side architecture or change your way of serving the file
//
function makePost() {
var url = "./rest-prefix/johndoe";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// User is NEVER prompted
}
}
var data = JSON.stringify({"name":"example.txt"});
xhr.send(data);
}
</script>
<button onclick="makePost()">TEST</button>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment