Download the picture in spark java

I went on to discuss spark github as well as the stack overflow to figure out how to upload files using spark and apache file uploads.

Now I want the user to be able to upload an image by click.

For example, my downloaded files are saved in the /tmp/imageName.jpg file on the server.

On the client side, I want to give the user the ability to upload a file when the user clicks on the hyperlink.

<a href="/image/path">click here</a> 

When the user clicks on the hyperlink, I call the function using the file path, but I cannot figure out how to send the image in response.

I know that HTML5 has a download attribute , but for this, the files must be stored in a shared folder on the server, which is not possible.

I went through a previous similar question, adding a replication attempt for my scenario without success

How to send a PNG QR code to the body of an HTTP response (using Spark)?

How to download a file using java spark?

Edit: I followed the link provided in response to download the image, but using response.raw (), I cannot get the answer

response.type("application/force-download");
        response.header("Content-Transfer-Encoding", "binary");
        response.header("Content-Disposition","attachment; filename=\"" + "xxx\"");//fileName);
        try {
        HttpServletResponse raw = response.raw();
        PrintWriter out = raw.getWriter();
        File f= new File("/tmp/Tulips.jpg");

        InputStream in = new FileInputStream(f);
        BufferedInputStream bin = new BufferedInputStream(in);
        DataInputStream din = new DataInputStream(bin);

        while(din.available() > 0){
            out.print(din.read());
            out.print("\n");
        }

        }
        catch (Exception e1) {
            e1.printStackTrace();
        }
        response.status(200);
        return response.raw();

Edit 2:

I'm not sure what the difference is between using response.body () and response.raw (). someFunction (). In any case, I can send the data back in response. Even if I write a simple response.body ("hello"), it will not be reflected in my answer.

Is there a difference in how the file will be counted as opposed to an image? Exam using the ImageIO class?

+4
source share
1 answer

, :

Service.java

get(API_CONTEXT + "/result/download", (request, response) -> {

        String key = request.queryParams("filepath");
        Path path = Paths.get("/tmp/"+key);
        byte[] data = null;
        try {
            data = Files.readAllBytes(path);
        } catch (Exception e1) {

            e1.printStackTrace();
        }

        HttpServletResponse raw = response.raw();
        response.header("Content-Disposition", "attachment; filename=image.jpg");
        response.type("application/force-download");
        try {
            raw.getOutputStream().write(data);
            raw.getOutputStream().flush();
            raw.getOutputStream().close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return raw;


   });

Angular

$scope.downloadImage= function(filepath) {
         console.log(filepath);
         window.open('/api/v1/result/download?filepath='+filepath,'_self','');
     }
+3

Source: https://habr.com/ru/post/1628577/


All Articles