브라우저에 PDF 파일을 표시하고 싶습니다. JS에서 pdf에 대한 경로가 있으며 Java에서 서블릿으로 PDF를 가져 오기 위해 호출하고 있습니다. 여기에 지금까지이 작업은 다음과 같습니다서블릿과 함께 브라우저에 PDF 표시
자바 스크립트 :
RequestManager.getJSON(Config.server + "getPDF.json?pdfPath=" + this.pathToPdfFile, (function(data){
$("#" + this.divId).append('<object id="' + this.pdfObjectId + '" data="' + data + '" type="application/pdf" width="600" height="800"></object>');
ResizeManager.addResizeHandler(this.pdfObjectId, this.divId, -10, -10);
}).bind(this));
자바 :
@RequestMapping("/getPDF")
public void pdfPathToServlet(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String pdfPath = request.getParameter("pdfPath");
if (pdfPath == null || pdfPath.equals(""))
throw new ServletException("Invalid or non-existent file parameter in UrlServlet servlet.");
if (pdfPath.indexOf(".pdf") == -1)
pdfPath += ".pdf";
File pdf = new File(pdfPath);
String pdfName = pdfPath.substring(pdfPath.lastIndexOf("/") + 1, pdfPath.length());
logger.debug(pdfName);
ServletOutputStream stream = null;
BufferedInputStream buf = null;
try
{
stream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename='" + pdfName + "'");
FileInputStream input = new FileInputStream(pdf);
response.setContentLength((int) pdf.length());
buf = new BufferedInputStream(input);
int readBytes = 0;
while ((readBytes = buf.read()) != -1)
stream.write(readBytes);
}
catch (IOException ioe)
{
throw new ServletException(ioe.getMessage());
}
finally
{
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}
}
내 문제는이 텍스트로 내 브라우저에서 바이너리 출력을 보여주는 것입니다.
내가 잘못하고 있는지 확실하지 않습니다. 인라인 대신 첨부 파일로 헤더를 변경하려고 시도했지만 동일한 결과가 나타났습니다. 나는 브라우저에서 보여주고 다운로드하지 않으려 고하면서 인라인을 원한다고 생각한다.
'application/pdf '를 반환하면 왜 URL이'.json'으로 끝나는 것입니까? –
@ Michael-O 솔직히 말해서, 나는 그것을 버릇없이 버렸습니다. 애플리케이션 작성 방법은 전체 애플리케이션이 실행되는 하나의 URL입니다. 그것은 실제로 변하지 않습니다. .json은 실제로 페이지가 실제로 보유하지 않는 다른 URL로 실제로 리디렉션되지 않도록하기 위해 보통 사용됩니다. – user856354
그것은 서블릿이 아닙니다. 그것은 스프링 MVC 컨트롤러 동작입니다. – BalusC