简体   繁体   中英

Unable to download file in Spring MVC

My Controller to which the request is mapped-

I return the value from AJAX, to the controller-


$.ajax({
        type: 'GET',
        dataType: 'json',
        contentType:"application/json",
        url:"/Putty/downloadProcess/?param="+param          
    });

@RequestMapping(value = "/downloadProcess", method = RequestMethod.GET)
    protected void download(@RequestParam("param") String value, HttpServletResponse response)
            throws ServletException, IOException {

        Properties prop = new Properties();
        InputStream input = new FileInputStream("config.properties");;
        prop.load(input);

        System.out.println(value);
        String path = prop.getProperty("path.MS1");
        String filepath= path.concat(value);
        System.out.println(filepath);

        File downloadFile = new File(filepath);
        FileInputStream inStream = new FileInputStream(downloadFile);


        String mimeType = "application/octet-stream";
        System.out.println("MIME type: " + mimeType);

        // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        // forces download
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile);
        response.setHeader(headerKey, headerValue);

        System.out.println(response);
        // obtains response's output stream
        OutputStream outStream = response.getOutputStream();

        byte[] buffer = new byte[4096];
        int bytesRead = -1;

        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }

        inStream.close();
        outStream.close();

This displays the filenames on my JSP


<c:forEach var="listValue" items="${files}">
        <label onClick="download('${listValue}')">${listValue}</label>
        <br>
    </c:forEach>

The problem is that, I can see the MIME type on my console, along with the value returned by AJAX- The filename. But I do not get the Download dialog box, when I click on the file names, displayed on my JSP. Am I not handling the requests properly or I am missing something else.

Thanks!

Try it

ServletOutputStream out = response.getOutputStream();
response.setContentType("application/octet-stream");

   if (file.isFile())
   {
        response.setHeader("Content-Disposition", "attachment;filename=\"" + downloadFile.getName() + "\"");
        try (FileInputStream inputStream = new FileInputStream(downloadFile ))
        {
            IOUtils.copy(inputStream, out);
        }
    }

The Open/Save dialogue appears by default so we can not force anything. It is a browser specific settings that you cant change on the client side.

For Mozilla Firefox example : 使用Mozilla Firefox浏览器,您将获得此弹出窗口

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