怎么取得文件类型如:text/html 请问:怎么取得文件类型如:text/html 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不是那个意思.不是从文件名分析.而是从文件内容例如:微软的幻灯片类型为:application/vnd.ms-powerpoint 如果是tomcat的话,在conf/web.xml里有,可以去读 不是在tomcat下.我是用的jdk请问,你知道吗? 去查阅相关HTTP报表表头那不分内容里面有涉及我记的在《servlet与jsp核心编程开发》里有你可以去看看 楼主的意思应该是servlet中有什么方法可以得到contentType这个内容,我想应该是有方法可以得到的,但是我不知道。 我的意思是:不用servlet是在JDK下怎么取Content Type /***我的解决办法*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;} }}/*方法很笨!但总算搞定!*/ 正则表达式取值问题 有没有现成的方法:取出一个字符串里第三个字符的位置 这次短些。自己实现的JMS远程方法调用。 hibernate中使用jdbc连接数据库的问题 Servlet输出到excel文件乱码问题? 帮忙,HELP! java自由作图 HashMap的存放的值能不能是一个hashTable? 求大神相助 JTable中的事件怎么加呀?急急急急死我了!! SWING是怎样像J2EE那样保持一个用户的会话? 怎样用java在网络上传送一个自定义包?(立结)
不是从文件名分析.
而是从文件内容
例如:
微软的幻灯片
类型为:application/vnd.ms-powerpoint
我是用的jdk
请问,你知道吗?
里面有涉及
我记的在《servlet与jsp核心编程开发》里有
你可以去看看
不用servlet
是在JDK下
怎么取Content Type
*我的解决办法
*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;}
}
}
/*
方法很笨!但总算搞定!
*/