我正在使用apache poi为xcel生成和下载开发代码。 LocalHost服务器和应用服务器是jboss。当我在localhost上运行代码时,会在jboss的部署文件夹中生成一个临时文件夹,并生成xcel,然后通过前端下载。我正在使用java spring angularjs和html。这在localhost上运行正常,但在app服务器上部署之后,xcel不会被下载,它会给出500:内部服务器错误。
angularjs控制器代码:
$scope.generateExcel=function(sDate,eDate,doc,search)
{
console.log("hello");
var sDate = document.getElementById('sD').value
var eDate = document.getElementById('eD').value
$scope.obj.sDate = sDate;
$scope.obj.eDate = eDate;
$scope.obj.iou = doc;
$scope.obj.du = search;
console.log($scope.obj);
$http.post('abc/generateExcel',$scope.obj).then(function()
{
//console.log(path);
$window.location.href="/ProjectName/file_name.xls";
})
.error(function()
{
console.log("Error!!");
});
};
java代码: //方法
public HttpServletResponse generateExcel ( HttpServletRequest request , HttpServletResponse response, String sD, String eD, String doc, String search)
{
//EXCEL GENERATION HERE
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=filename.xls");
//Path Specification
String path = request.getRealPath("/file_name.xls");
//System.out.println("Here...");
System.out.println(path);
FileOutputStream fileOut2 = new FileOutputStream(path);
workbook.write(fileOut2);
/*returning response*/
}
答案 0 :(得分:0)
除非知道你在服务器端遇到的错误,否则很难回答这个问题。将您的服务器代码放在try-catch块中。重新运行代码,并检查服务器日志。将它们粘贴在这里。
try{
String path = request.getRealPath("/file_name.xls");
//System.out.println("Here...");
System.out.println(path);
FileOutputStream fileOut2 = new FileOutputStream(path);
workbook.write(fileOut2);
} catch(Exception e){
e.printStackTrace(); // this should print some error in server logs
}