서버쪽에 Excel 파일이 있습니다. 서블릿을 사용하여 클라이언트 측 브라우저에서 어떻게 표시 할 수 있습니까?서블릿을 사용하여 서버에서 Excel 파일 다운로드
미리 감사드립니다. 점에
서버쪽에 Excel 파일이 있습니다. 서블릿을 사용하여 클라이언트 측 브라우저에서 어떻게 표시 할 수 있습니까?서블릿을 사용하여 서버에서 Excel 파일 다운로드
미리 감사드립니다. 점에
: 그냥 어떻게 든의 InputStream
를 얻을 수 (FileInputStream
이 적합) 및 응답의 OutputStream
보통 Java IO 방법에 씁니다. 그것은 기본적으로 모두입니다. 올바른 응답 헤더를 설정하기 만하면되므로 브라우저가이를 어떻게 처리하는지 이해할 수 있습니다. Content-Type
헤더는 어떤 종류의 파일인지 웹 브라우저에 알려서 브라우저가 열어 볼 응용 프로그램을 알도록합니다.
는 여기에 킥오프 예제 :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
File file = new File("/path/to/files", filename);
response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
response.setHeader("Content-Length", file.length());
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
} finally {
if (output != null) try { output.close(); } catch (IOException ignore) {}
if (input != null) try { input.close(); } catch (IOException ignore) {}
}
}
지도 /files/*
의 url-pattern
에 web.xml
이 서블릿은 당신이 http://example.com/contextname/files/filename.xls
하여 엑셀 파일을 얻을 수 있도록.
실제로 아직 평균 servletcontainer 인식 기본적으로이 아닌 xlsx
파일을, 만약합니다 (ServletContext#getMimeType()
후 대신 원하는 xlsx
콘텐츠 형식의 application/octet-stream
을 반환은), 당신은 web.xml
에 다음 항목을 추가해야합니다 뿐만 아니라 :
<mime-mapping>
<extension>xlsx</extension>
<mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>
파일 서블릿의 고급 예를 들어, 당신은 각 다운로드뿐만 아니라 다시 시작에서 유용뿐만 아니라, 자신이 지원 this article 찾을 수 있습니다.