请问:怎么取得文件类型如:text/html
解决方案 »
- 关于获取网页源代码的问题!!
- 如何读取一个文件,用某种特定的编码写入另一个文件
- 请教有没有socket通信层开发的例子,博客也行,要有同步处理,异步提供回调方法等的,不要一个简单的聊天室例子
- 用JAVA生成的密钥是什么格式,怎么输出啊
- 求救,急jsp+flash实现.swf文件的播放
- 为什么2个日历变量的结果会不同??
- 帮忙看看socket客户端问题,谢谢!
- 怎么在servlet里取得远程主机上的一幅图片?比如sohu网站上的一幅图片??
- 使用url如何获取动态网页(*.asp *.jsp)的内容
- java面向对象编程指南-100分奉上
- 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;}
}
}
/*
方法很笨!但总算搞定!
*/