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

时间:2014-10-11 06:40:49

标签: java servlets download zip

下面给出的用于将文件下载到客户端计算机的代码。我正在尝试使用java servlet将文件下载到客户端计算机。但它没有下载我在控制台中检查但没有发生错误

尝试{

                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();
            }

2 个答案:

答案 0 :(得分:1)

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');

答案 1 :(得分:0)

使用out.write并将byte stream中的文件传递给out.write。它会保存文件而不是打开