如何在java

时间:2015-06-08 03:57:29

标签: java servlets

我正在尝试将服务器中生成的报告下载到本地计算机,因为我已经写了servlet并从我的servelt调用jsp。但它不是在调用我的servlet ...我添加了一些S.O.P进行测试,我没有在日志中获得s.o.ps并且屏幕显示为空白。

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    try {
        System.out.println("In RRR DownLoadServlet");
        outs = response.getOutputStream();
        String action = request.getParameter("action");
        String strInfodom = "DMINFO88";
        //String filename = request.getParameter("filename");
        String filename = "/Report_Income Statement.xls";

        if (action.equals("6")) {
            Vector<String> vProperties = new Vector<String>();
            vProperties.addElement("DOCUMENT_UPLOAD_SAVE");
            vProperties.addElement("DOCUMENT_UPLOAD_TEMP");
          //  Properties prop = SMSServices.getConfiguration(vProperties);
          //  String strUploadSave = (String) prop.get("DOCUMENT_UPLOAD_SAVE");
        //    String strUploadTemp = (String) prop.get("DOCUMENT_UPLOAD_TEMP");

            String completePath = RRRConstants.RRR_PATH+RRRConstants.OUTPUT;

            System.out.println("Report File path ::::::::::::::::  "+completePath);
            if (completePath != null) {
                byte arrByte[] = new byte[1024];
                int readBytes = -1;
                response.setContentType("application/x-download");
                response.setHeader("Content-Disposition", "attachment; filename=" + completePath.substring(completePath.lastIndexOf("/") + 1));

                fin = new FileInputStream(new File(completePath));
                BufferedInputStream bout = new BufferedInputStream(fin);
                while ((readBytes = bout.read(arrByte)) > 0) {
                    outs.write(arrByte, 0, readBytes);
                }
                outs.flush();
            }
        }
    }
    catch (Exception e) {
         e.printStackTrace();
    }
    finally {
        try {
            if (outs != null)
                outs.close();
            if (fin != null)
                fin.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

jsp代码:

<%
  String infodom = request.getParameter("infodom");
  String user = (String) session.getAttribute("gsUsrID");
  String strURL = "";
  FileHandler objFileHandler = new FileHandler(null);
  WebServerInfo objWebServer = objFileHandler.getPrimaryWebServerInfo();
  String protocol = objWebServer.getServletProtocol();
  try
  {
     strURL = protocol+ "://" +                     getServletConfig().getServletContext().getInitParameter("FIC_WEBSERVER_IP") + ":" +  getServletConfig().getServletContext().getInitParameter("FIC_WEBSERVER_PORT") +  request.getContextPath() + "/RRRDownLoadServlet?infodom="+infodom+"&filename=";


   }
   catch(Exception e) 
   {
    System.out.println(" [Download.jsp] Exception "+e);
   }
%> 


function downloareport()
{

alert("downloading");
var filename = "/Report_Income Statement.xls";
downloadExcel(filename);

}

function downloadExcel(excelFile)
{

   window.location.href = "<%=strURL%>"+ excelFile+"&mode=excel";
 }  

2 个答案:

答案 0 :(得分:0)

jsp代码包含一些javascript。检查以下几点:

在javascript中;只写函数体并不会自动调用它,必须在某处调用函数。

此外,似乎缺少java-scripts标签。

答案 1 :(得分:0)

来自JSP的请求是get,而不是post。在servlet中使用doGet(...){}而不是doPost(...){...}