[英]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.