有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何访问servlet和下载附件?

我有以下代码片段,尝试对我的servlet进行HTTP调用:

    try {
        // Construct data
        String data = URLEncoder.encode("rpt_type", "UTF-8") + "=" + URLEncoder.encode(reportType, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_project", "UTF-8") + "=" + URLEncoder.encode(reportProject, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_mrv_creator", "UTF-8") + "=" + URLEncoder.encode(reportMrvCreator, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_gi_recipient", "UTF-8") + "=" + URLEncoder.encode(reportGiRecipient, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_plant", "UTF-8") + "=" + URLEncoder.encode(reportPlant, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_sloc", "UTF-8") + "=" + URLEncoder.encode(reportStorageLoc, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_gi_no", "UTF-8") + "=" + URLEncoder.encode(reportGiNo, "UTF-8");
        data += "&" + URLEncoder.encode("date_sap_gi_fr", "UTF-8") + "=" + URLEncoder.encode(reportDateGiFrom, "UTF-8");
        data += "&" + URLEncoder.encode("date_sap_gi_to", "UTF-8") + "=" + URLEncoder.encode(reportDateGiTo, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_partno", "UTF-8") + "=" + URLEncoder.encode(reportPartNo, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_so_no", "UTF-8") + "=" + URLEncoder.encode(reportSvcOrderNo, "UTF-8");
        data += "&" + URLEncoder.encode("date_scan_fr", "UTF-8") + "=" + URLEncoder.encode(reportDateScanFrom, "UTF-8");
        data += "&" + URLEncoder.encode("date_scan_to", "UTF-8") + "=" + URLEncoder.encode(reportDateScanTo, "UTF-8");
        System.out.println("[data]\n" + data);

        // Send data
        String urlString = "http://localhost:8080/aerobook/GIStatusReportDownload?" + data;
        System.out.println("[url] " + urlString);
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        //conn.setDoOutput(true);
        //OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        //wr.write(data);
        //wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        //wr.close();
        rd.close();
    } catch (Exception e) {
    }

我的调试输出:

[data]
rpt_type=d&rpt_project=aaa&rpt_mrv_creator=bbb&rpt_gi_recipient=ccc&rpt_plant=ddd&rpt_sloc=eee&rpt_gi_no=fff&date_sap_gi_fr=02%2F05%2F2012&date_sap_gi_to=03%2F05%2F2012&rpt_partno=ggg&rpt_so_no=hhh&date_scan_fr=26%2F05%2F2012&date_scan_to=31%2F05%2F2012
[url] http://localhost:8080/aerobook/GIStatusReportDownload?rpt_type=d&rpt_project=aaa&rpt_mrv_creator=bbb&rpt_gi_recipient=ccc&rpt_plant=ddd&rpt_sloc=eee&rpt_gi_no=fff&date_sap_gi_fr=02%2F05%2F2012&date_sap_gi_to=03%2F05%2F2012&rpt_partno=ggg&rpt_so_no=hhh&date_scan_fr=26%2F05%2F2012&date_scan_to=31%2F05%2F2012

在我的servlet上(在上面代码的单独文件中),我生成了一个Excel文件供下载:

    res.setContentType(sContentType);
    res.setHeader("Content-Disposition", "attachment;filename=\"" + sExcelFileName + "\"");
    OutputStream oOutStrm = res.getOutputStream();
    wbBook.write(oOutStrm);
    oOutStrm.close();

这里我的问题是,从我的代码生成的URL(如上面的调试输出所示),我可以访问我的servlet,并设法获得“另存为”对话框

我想获取生成的文件内容,以便在代码中使用。是否有任何方法可以从我的代码中获取附件,以字节流或任何其他格式


编辑#3:清理顶部


共 (4) 个答案

  1. # 1 楼答案

    检查wbBook.write(oOutStrm);是否有任何内容已写入outputStream,并且在关闭它之前需要调用oOutStrm.flash()

  2. # 2 楼答案

    在浏览器中输入URI时,您正在执行GET请求。但是,客户端Java代码会生成POST请求,并在主体中发送参数,而不是URI

    您可能需要查看HTTP跟踪并进行比较

  3. # 3 楼答案

    I want to get the contents of the excel file on my code, but so far it's not working.

    我在代码中没有发现错误
    我相信您希望将内容从输入流转换为HSSFWorkbook对象
    下面的代码片段将对您有所帮助

    java.net.URLConnection conn = url.openConnection();  
    java.io.InputStream is = conn.getInputStream();  
    org.apache.poi.hssf.usermodel.HSSFWorkbook workBook = new org.apache.poi.hssf.usermodel.HSSFWorkbook( is );  
    System.out.println( "Number of Sheets: " + workBook.getNumberOfSheets() );  
    org.apache.poi.hssf.usermodel.HSSFSheet sheet = workBook.getSheetAt( 0 );  
    System.out.println( "Sheet Name: " + sheet.getSheetName() );  
    
    // rest of your code to handle the captured workBook
    // cheers
    
  4. # 4 楼答案

    我怀疑问题出在哪里

    我打赌resHttpServletResponse,它返回适合在响应中写入二进制数据的ServletOutputStream。servlet容器不编码二进制数据

    勾选API

    所以,除了文件名,你可能什么也得不到

    在Servlet中尝试

    FileOutputStream stream = new FileOutputStream("c:/excel/Book1.xls");
    workBook.write(stream);