weblogic下載不提示下載信息框 getOutputStream()報異常

這個問題就是weblogic自作聰明,把文件按照非二進制來解析,結果成功了,就直接顯示了而已,

解決這個問題要給頭文件設置一個參數~

 response.setContentType("application/x-download");


另外,getOutputStream()在調用的時候報錯了,其實是因爲這個流沒有關閉而已。

例子:

<%@ page language="java"  pageEncoding="utf-8"%>
<%@ page trimDirectiveWhitespaces="true" %> 
<%
/*
  String url = (String)request.getAttribute("url");
  System.out.println(url);
  response.setContentType("application/x-download");//設置爲下載application/x-download   
  response.addHeader("Content-Disposition","attachment;filename=text.xml"); 
  
  try   
  {   
  RequestDispatcher dis = application.getRequestDispatcher(url);   
  if(dis!= null)   
  {   
  dis.forward(request,response);   
  }   
  response.flushBuffer();   
  }   
  catch(Exception e)   
  {   
  e.printStackTrace();   
  }   
  finally   
  {   
   
  }  
  */ 
%> 
<%
 out.clear(); 
 response.reset(); 
 String url = (String)request.getAttribute("url");
 response.setContentType("application/x-download");
 response.addHeader("Content-Disposition", "attachment;filename="
   + url); // 
 response.setCharacterEncoding("utf-8");
 java.io.OutputStream outp = null;
 java.io.FileInputStream in = null;
 try {
  outp = response.getOutputStream();
  in = new java.io.FileInputStream(url);
  byte[] b = new byte[1024];
  int i = 0;

  while ((i = in.read(b)) > 0) {
   outp.write(b, 0, i);
  }

 } catch (Exception e) {
  System.out.println("Error!");
  e.printStackTrace();
 } finally {
 outp.flush();
 outp.close();
  if (in != null) {
   in.close();
   in = null;
  }
 }
%>






 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章