简体   繁体   English

如何从客户端计算机下载zip文件,而不是使用Java在浏览器中打开文件?

[英]how to download zip-file from client machine instead of opening file in browser using java?

the given below code for downloading file to client machine. 下面提供的用于将文件下载到客户端计算机的代码。 I am trying to download file to client machine using java servlet. 我正在尝试使用Java Servlet将文件下载到客户端计算机。 but it's not downloaded i checked in console but no error occur 但是没有下载,但我在控制台中检查了但没有发生错误

try { 尝试{

                FileInputStream fileInputStream = new FileInputStream(downloadPath);
                response.setContentType("application/zip");
                response.setHeader("Content-Disposition", "attachment; filename=\""+dndfilename);
                response.setContentLength(fileInputStream.available());
                int i;
                while ((i = fileInputStream.read()) != -1) {
                    response.getOutputStream().write(i);
//                  System.out.println("Result: " +i);
                }
                response.getOutputStream().flush();
                response.getOutputStream().close();
                fileInputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
Actually i send the request in ajax so only it's not working after that i change the request.




 var serverURL = hURL + "/rup";
            var parameters = "requestType=port&subRequestType=portScen&userName=" + userName + "&scen=" + selectedScen;
            $.ajax({
                type: "POST",
                data: parameters,
                url: serverURL,
                cache:false,
                dataType:"html",
                success: function() {
                    updateStatusMessage("success", "Scen port successfully");
                },
                async:false
            });

Solution of the problem:

var serverURL = hURL + "/rpp?requestType=port&subRequestType=portScen&userName=" + userName + "&scen=" + selectedScen;
window.open(serverURL, '_blank');

use out.write and pass file in byte Stream to out.write. 使用out.write并将字节流中的文件传递给out.write。 It will save file rather opening 它将保存文件而不是打开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Liferay 将多个文件下载为 zip 文件? - How to download multiple files as a zip-file using Liferay? 使用 Java 从 S3 并行下载多个文件到一个 zip 文件 - Download multiple files in parallel to a zip-file from S3 using Java 解压缩后打开/下载zip文件时出现问题(Java / ZipInputStream) - Problem opening/downloading zip-file after decompression (Java/ZipInputStream) 在Java中创建Zip文件而不使用byte [] - Create Zip-File in java without using byte[] 为什么我在使用java.util.zip.ZipFile打开一个空的ZIP文件时遇到异常? - Why I get an Exception on opening an empty ZIP-file with java.util.zip.ZipFile? 下载文件,而不是使用struts2在浏览器中打开 - download file instead of opening in browser using struts2 在名为zip-file的文件夹中使用骆驼解压缩文件 - Unzip file using camel in folder named like zip-file Java-使用子文件夹从不同位置创建包含多个文件的Zip文件 - Java - Create Zip-file with multiple files from different locations with subfolders Java:递归地将文件添加到zip文件但没有完整路径 - Java: Add files to zip-file recursively but without full path 在Java中运行时从zip文件读取ESRI shapefile - DataStoreFinder.getDataStore(connectParameters)返回null - Reading an ESRI shapefile from a zip-file during Runtime in Java - DataStoreFinder.getDataStore(connectParameters) returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM