简体   繁体   English

如何使用Java将文件下载到桌面

[英]How to Download the file to desktop in java

I am trying to to download the report generated in server to the local machine for that i have written a servlet and calling the servelt from my jsp . 我试图将服务器中生成的报告下载到本地计算机,因为我已经编写了一个servlet并从我的jsp调用servelt But its not ont invoking my servlet ... i have added some SOP for testing and i am not getting those sops in log and and the screen display blank. 但是它并没有调用我的servlet ...我已经添加了一些SOP进行测试,但是我并没有在日志中获取这些sops ,并且屏幕显示空白。

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 code : 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";
 }  

The jsp code contains some javascript. 该jsp代码包含一些javascript。 Check these points: 检查以下几点:

In javascript; 在javascript; writing just function body doesn't automatically invoke that, one has to make call to the function somewhere. 仅编写函数体并不会自动调用该函数体,而必须在某处调用该函数。

Also tags for java-scripts seems missing. Java脚本的标签也似乎丢失了。

The request from the JSP is get, not post. 来自JSP的请求是get,而不是post。 Use doGet(...){} instead of doPost(...){...} in servlet. 在servlet中使用doGet(...){}代替doPost(...){...}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM