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

butnet 2006-08-26 11:38:57
请问:怎么取得文件类型如:text/html
...全文
382 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jayfantsy 2006-10-04
  • 打赏
  • 举报
回复
楼上,我看不懂啊..菜鸟难啊..
butnet 2006-10-03
  • 打赏
  • 举报
回复
/**
*我的解决办法
*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;}
}
}
/*
方法很笨!但总算搞定!
*/
butnet 2006-08-27
  • 打赏
  • 举报
回复
我的意思是:
不用servlet
是在JDK下
怎么取Content Type
wjs2338 2006-08-26
  • 打赏
  • 举报
回复
楼主的意思应该是servlet中有什么方法可以得到contentType这个内容,我想应该是有方法可以得到的,但是我不知道。
javajiao 2006-08-26
  • 打赏
  • 举报
回复
去查阅相关HTTP报表表头那不分内容
里面有涉及
我记的在《servlet与jsp核心编程开发》里有
你可以去看看
zhangj0571 2006-08-26
  • 打赏
  • 举报
回复
莫非是 用正则表达式去模式匹配???
butnet 2006-08-26
  • 打赏
  • 举报
回复
不是在tomcat下.
我是用的jdk
请问,你知道吗?
liujin4049 2006-08-26
  • 打赏
  • 举报
回复
如果是tomcat的话,在conf/web.xml里有,可以去读
butnet 2006-08-26
  • 打赏
  • 举报
回复
不是那个意思.
不是从文件名分析.
而是从文件内容
例如:
微软的幻灯片
类型为:application/vnd.ms-powerpoint
meadking 2006-08-26
  • 打赏
  • 举报
回复
根据文件名后缀

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧