topgun_ivard
topgun_ivard

Reputation: 8564

HttpServletResponse prompt for file name on save

I used code similar to something below to return a zip file as an attachment to a SpringMVC request. The whole thing works great, I am able to download a file called hello.zip when I make a request to localhost/app/getZip.

My question here is, how can I prompt the user to enter a file name. Currently on FireFox25.0 it automatically assumes the name to be "hello.zip" without the provision to change the file name on Open or Save options.

    @RequestMapping("getZip")
    public void getZip(HttpServletResponse response)
    {
        OutputStream ouputStream;
        try {
            String content = "hello World";
            String archive_name = "hello.zip";
            ouputStream = response.getOutputStream();
            ZipOutputStream out = new ZipOutputStream(ouputStream);
            out.putNextEntry(new ZipEntry(“filename”));
            out.write(content);
            response.setContentType("application/zip");
            response.addHeader("Content-Disposition", "attachment; filename="+ archive_name);
            out.finish();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

TL;DR: Using HttpServletResponse I want user to provide a file name instead of passing one in the Header.

Upvotes: 2

Views: 3518

Answers (1)

ctesniere
ctesniere

Reputation: 131

with method to RequestMethod.GET
URL : http://localhost/app/getZip?filename=hello.zip

@RequestMapping(value = "getZip/{filename}", method = RequestMethod.GET)
public void getZip(HttpServletResponse response, @PathVariable String filename)
{
    OutputStream ouputStream;
    try {
        String content = "hello World";
        String archive_name = "hello.zip";
        ouputStream = response.getOutputStream();
        ZipOutputStream out = new ZipOutputStream(ouputStream);
        out.putNextEntry(new ZipEntry("filename"));
        out.write(content);
        response.setContentType("application/zip");
        response.addHeader("Content-Disposition", "attachment; filename="+ archive_name);
        out.finish();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 3

Related Questions