java里打开文件好像都是根据FileOutputStream(file)里file的扩展名来选择不同的打开类型的,现在我想让所有的文件都用html打开应该怎么办?  注意:不是把扩展名改成html,不是要那个效果。

解决方案 »

  1.   

    pdf直接在浏览器以插件打开是不是你要的效果?还是所有格式文件你都处理为html后输出?
      

  2.   

    package test;import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class PDFServlet extends HttpServlet { private static final long serialVersionUID = -3065671125866266804L; public PDFServlet() {
    super();
    } public void destroy() {
    super.destroy();
    } public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("application/pdf"); //要什么类型的?改这
    FileInputStream in = new FileInputStream(new File("d:/1.pdf"));
    OutputStream out = response.getOutputStream();
    byte[] b = new byte[512];
    while ((in.read(b)) != -1) {
    out.write(b);
    }
    out.flush();
    in.close();
    out.close();
    } public void init() throws ServletException { }}
      

  3.   

    response.setContentType("text/html"); 
    设置这个后不管你输出流什么都是在浏览器中打开了。
      

  4.   

    [Quote=引用 4 楼 victor_woo 的回复:]
    那你就把所有文件的插件都安装上就可以了
    ........................