请问:怎么取得文件类型如:text/html

解决方案 »

  1.   

    不是那个意思.
    不是从文件名分析.
    而是从文件内容
    例如:
    微软的幻灯片
    类型为:application/vnd.ms-powerpoint
      

  2.   

    如果是tomcat的话,在conf/web.xml里有,可以去读
      

  3.   

    不是在tomcat下.
    我是用的jdk
    请问,你知道吗?
      

  4.   

    去查阅相关HTTP报表表头那不分内容
    里面有涉及
    我记的在《servlet与jsp核心编程开发》里有
    你可以去看看
      

  5.   

    楼主的意思应该是servlet中有什么方法可以得到contentType这个内容,我想应该是有方法可以得到的,但是我不知道。
      

  6.   

    我的意思是:
    不用servlet
    是在JDK下
    怎么取Content Type
      

  7.   

    /**
    *我的解决办法
    *FileType.java
    */
    package FileType;
    import java.io.*;
    public class FileType
    {
    FileReader f;
    BufferedReader in;
    String trueEnd;
    public FileType(String End) throws Exception
    {
    try{
    f=new FileReader("alltype.txt");
    in=new BufferedReader(f);
    trueEnd=End;
    }catch(Exception e){Error.error("初始化mail.FileType类失败"+e);throw new Exception();}
    }
    public String findType() throws Exception
    {
    try{
    String type="application/octet-stream";
    String tempType=null;
    String end="";
    while(true)
    {
    tempType=readType();
    if(tempType==null)return type;
    end=readEnd();
    if(end.compareTo(trueEnd)==0)return tempType;
    }
    }catch(Exception e){Error.error("mail.FileType.findType()出错:"+e);return null;}
    }
    private String readType() throws Exception
    {
    try{
    do{
    String line=new String("");
    line=in.readLine();
    if(line==null)return null;
    if(line.indexOf("/")!=-1)
    {
    line=line.substring(line.lastIndexOf("\\")+1);
    line=line.substring(0,line.length()-1);
    return line;
    }
    }while(true);
    }catch(Exception e){Error.error("mail.FileType.readType()出错:"+e);return null;}
    }//readType
    private String readEnd() throws Exception
    {
    try{
    String line=new String("");
    do{
    line=in.readLine();
    if(line==null)return null;
    if(line.indexOf(".")!=-1)
    {
    line=line.substring(line.lastIndexOf("."));
    line=line.substring(0,line.length()-1);
    return line;
    }
    }while(true);
    }catch(Exception e){Error.error("mail.FileType.readEnd()出错:"+e);return null;}
    }//readEnd
    }/**
    *getAllFileType.java
    */
    package FileType;
    import java.io.*;
    class getAllFileType{
    BufferedReader in;
    BufferedWriter out;
    public getAllFileType() throws Exception
    {
    start();
    }
    private void start() throws Exception
    {
    try{
    FileWriter bat=new FileWriter("IntToChar.bat");
    out=new BufferedWriter(bat);
    out.write("type temptype.txt>alltype.txt\r\ndel temptype.txt\r\ndel IntToChar.bat");
    out.close();
    bat.close();
    Runtime runThread=Runtime.getRuntime();
    Process childTh=runThread.exec("regedit /e temptype.txt \"HKEY_CLASSES_ROOT\\MIME\\Database\\Content Type\"");
    childTh.waitFor();
    childTh=runThread.exec("IntToChar.bat");
    childTh.waitFor();
    FileReader f=new FileReader("alltype.txt");
    in=new BufferedReader(f);
    FileWriter f1=new FileWriter("All Types.txt");
    out=new BufferedWriter(f1);
    while(true)
    {
    String w=readType();
    if(w==null){in.close();out.close();return;}
    out.write(w);
    w=readEnd();
    if(w==null){in.close();out.close();return;}
    out.newLine();
    out.write(w);
    out.newLine();
    }
    }catch(Exception e){Error.error("mail.getAllFileType.start()出错:"+e);}
    }
    private String readType() throws Exception
    {
    try{
    do{
    String line=new String("");
    line=in.readLine();
    if(line==null)return null;
    if(line.indexOf("/")!=-1)
    {
    line=line.substring(line.lastIndexOf("\\")+1);
    line=line.substring(0,line.length()-1);
    return line;
    }
    }while(true);
    }catch(Exception e){Error.error("mail.getAllFileType.start()出错:"+e);return null;}
    }//readType
    private String readEnd() throws Exception
    {
    try{
    String line=new String("");
    do{
    line=in.readLine();
    if(line==null)return null;
    if(line.indexOf(".")!=-1)
    {
    line=line.substring(line.lastIndexOf("."));
    line=line.substring(0,line.length()-1);
    return line;
    }
    }while(true);
    }catch(Exception e){Error.error("mail.getAllFileType.start()出错:"+e);return null;}
    }
    }
    /*
    方法很笨!但总算搞定!
    */