Java 如何在SpringContoller中为下载文件设置客户端文件夹路径(如D://新文件夹)?

Java 如何在SpringContoller中为下载文件设置客户端文件夹路径(如D://新文件夹)?,java,spring,spring-mvc,tomcat,spring-data,Java,Spring,Spring Mvc,Tomcat,Spring Data,我有一个spring控制器,我想下载特定路径中的文件,如D://或K://,但现在默认情况下它将下载到下载文件夹中。 我正在从/WEB-INF/文件夹中获取文件(服务器端位于Tomcat文件夹中),我想在客户机D:\drive中写入,请查看我下面的代码有问题,请告诉我。我正在使用google crome 提前谢谢 public void downloadFile(HttpServletRequest request, HttpServletResponse re


我有一个spring控制器,我想下载特定路径中的文件,如D://或K://,但现在默认情况下它将下载到下载文件夹中。
我正在从/WEB-INF/文件夹中获取文件(服务器端位于Tomcat文件夹中),我想在客户机D:\drive中写入,请查看我下面的代码有问题,请告诉我。我正在使用google crome
提前谢谢

 public void downloadFile(HttpServletRequest request,
                HttpServletResponse response) throws IOException {
            //ServletContext context = request.getServletContext();
            String filePath = request.getSession().getServletContext().getRealPath("/WEB-INF/") + "/"+"out.json";

             // get absolute path of the application
            ServletContext context = request.getServletContext();
            String appPath = context.getRealPath("");
            System.out.println("appPath = " + appPath);

            // construct the complete absolute path of the file
            //String fullPath = appPath + filePath;      
            File downloadFile = new File(filePath);
            FileInputStream inputStream = new FileInputStream(downloadFile);

            // get MIME type of the file
            String mimeType = context.getMimeType(filePath);
            if (mimeType == null) {
                // set to binary type if MIME mapping not found
                mimeType = "application/octet-stream";
            }
            System.out.println("MIME type: " + mimeType);

            // set content attributes for the response
            response.setContentType(mimeType);
            response.setContentLength((int) downloadFile.length());

            // set headers for the response
            String headerKey = "Content-Disposition";
            String headerValue = String.format("attachment; filename=\"%s\"",
                    downloadFile.getName());
            System.out.println("downloadFile.getName()" + downloadFile.getName());
            response.setHeader(headerKey, headerValue);

            // get output stream of the response
            OutputStream outStream = response.getOutputStream();

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

            // write bytes read from the input stream into the output stream
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, bytesRead);
            }

            inputStream.close();
            outStream.close();
        }

您无法决定客户端将下载的文件保存在服务器端的何处-事实上,您甚至无法影响客户端是否将其保存在任何位置!这由客户(如Chrome)决定


有关如何在Chrome中更改默认下载位置的信息,请参阅。

实际客户端发送其文件夹位置。。我们只想在那个位置下载文件。按设计,浏览器不是这样工作的。你不能按你的要求去做。