, 당신은 response.setContentType("application/pdf");
을 추가하고 출력으로 파일의 바이트를 쓰기 아파치 ..
public class ReportSintesiServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
Lookup<IReportGeneration> l = new Lookup<IReportGeneration>(true, IReportGeneration.class);
IReportGeneration g = null;
try {
g = l.lookup();
} catch (Exception ex) {
return;
}
if (g != null) {
String param = request.getParameter("idc");
System.out.println("genero il report per idc " + param);
Integer idc = Integer.parseInt(param);
byte[] doc = (byte[]) g.generaSintesiAccordoIniziale(idc);
if (doc != null) {
FilePathManager fpm = new FilePathManager();
String pathtofile = fpm.pathToNuovaSintesi(idc);
File temp = new File(pathtofile);
FileUtil.writeBytesToFile(temp, doc);
response.addHeader("Content-Disposition", "attachment; filename=" + temp.getName());
response.setContentLength((int) temp.length());
FileInputStream fileInputStream = new FileInputStream(temp);
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
PS입니다 : 나는 이 경우 도움이되지 않은 일부 코드가 제거되었으므로 오류 관리를위한 논리의 모든 부분을 놓칠 수 있습니다.
출처
2014-07-08 19:20:33
LMG
서버가 적절한 Content-type 헤더를 출력하지 않아서 디스플레이 환경 (브라우저?)이 pdf를 text/plain으로 처리합니다. –
맞아! 파이어 폭스에서는이 이미지 결과가 있고 IE에서는 파일 *을 다운로드합니다. txt. 서버에 무언가를 설치하거나 권한을 변경해야합니까? 고맙습니다! –
서블릿이 어떤 시점에서'Content-type : application/pdf' 헤더를 출력해야합니다 ... –