나는이 내 서블릿에 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
(위의 코드는 별도의 파일에) 나는 다운로드 Excel 파일을 생성합니다
res.setContentType(sContentType);
res.setHeader("Content-Disposition", "attachment;filename=\"" + sExcelFileName + "\"");
OutputStream oOutStrm = res.getOutputStream();
wbBook.write(oOutStrm);
oOutStrm.close();
여기 내 문제는 (위의 디버그 출력 참조) 내 코드에 의해 생성 된 URL에서, 내 서블릿 a를 액세스 할 수있다 그러면 Save-As 대화 상자를 얻을 수 있습니다.
내 코드 내에서 사용하기 위해 생성 된 파일의 내용을 얻고 싶습니다. 내 코드에서 바이트 스트림이나 다른 형식으로 첨부 파일을 가져올 수있는 방법이 있습니까?
편집 # 3 : 또한 가까운 그 전에 oOutStrm.flash()
를 호출 할 필요가 아무것도의 OutputStream에 기록되었는지 여부를 상단
브라우저에서 URL을 확인 했습니까? –
예. 위의 코드 섹션에서'System.out.println (data);'문자열'key1 = value1 & key2 = value2'을 생성했습니다. 내가 한 일은 내가 "?"라고 덧붙였다. 내 URL에이 문자열이옵니다. 내가 얻은 결과는'http : // localhost/MyProject/MyServlet? key1 = value1 & key2 = value2'입니다. 이 주소를 브라우저 주소창에 입력하면 엑셀 파일을 다운로드 할 수있었습니다. 내 코드에서 Excel 파일의 내용을 가져오고 싶지만 지금까지 작동하지 않습니다. – ohseekay
확인. 이 샘플을 확인하십시오. http://www.roseindia.net/java/example/java/io/file-url-download.shtml –