由于项目的需要,需要写一个解析文件的类。
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.Hashtable;
import com.nokia.mms.*;
/**
*
* @author JavaAlpha 解析用户发送的自定义文件,生成原始内容文件
*/
public class SeedAudit
{
MMMessage mms = new MMMessage();
MMDecoder decoder = new MMDecoder();
MMContent content = new MMContent();
public SeedAudit(String file)
{
// 将MMS文件写到byte数组中
byte[] mmsFile = readFile(file);
// 将数组set到content中
content.setContent(mmsFile, 0, mmsFile.length);
decoder.setMessage(mmsFile);
try
{
decoder.decodeMessage();
mms = decoder.getMessage();
storeContents(mms);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MMDecoderException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
/*// 获取MMS文件的id
String id = content.getContentId();
// 获取MMS文件的type
String type = content.getType();
// 获取MMS文件长度
int length = content.getLength();
System.out.println("id--" + id);
System.out.println("type--" + type);
for (int i = 0; i < length; i++)
{
try
{
if (type.compareTo(IMMConstants.CT_TEXT_PLAIN) == 0)
{
// 如果文件类型是文本文件
content.saveToFile(id + ".txt");
} else if (type.compareTo(IMMConstants.CT_IMAGE_JPEG) == 0)
{
// 如果文件类型是jpg文件
content.saveToFile(id + ".jpg");
} else if (type.compareTo(IMMConstants.CT_IMAGE_GIF) == 0)
{
// 如果文件类型是gif文件
content.saveToFile(id + ".gif");
} else
{
content.saveToFile(id);
}
} catch (IOException e)
{
e.printStackTrace();
}
}*/
}
private void storeContents(MMMessage mm) throws IOException
{
MMContent mmContent = null;
String path = getPath();
for (int j = 0; j < mm.getNumContents(); j++)
{
mmContent = mm.getContent(j);
/*System.out.println(" Content " + j + " --> ID = "
+ mmContent.getContentId() + ", TYPE = "
+ mmContent.getType() + ", LENGTH = "
+ mmContent.getLength());*/
if ((mmContent.getType()).compareTo(IMMConstants.CT_IMAGE_WBMP) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".wbmp");
else if ((mmContent.getType())
.compareTo(IMMConstants.CT_TEXT_PLAIN) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".txt");
else if ((mmContent.getType())
.compareTo(IMMConstants.CT_APPLICATION_SMIL) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".smil");
else if ((mmContent.getType())
.compareTo(IMMConstants.CT_IMAGE_JPEG) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".jpg");
else if ((mmContent.getType()).compareTo(IMMConstants.CT_IMAGE_GIF) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".gif");
else if ((mmContent.getType()).compareTo("audio/wav") == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".wav");
else
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()));
}
}
private String cleanString(String str)
{
String result;
if ((str.charAt(0) == '<') && (str.charAt(str.length() - 1) == '>'))
result = str.substring(1, str.length() - 1);
else
result = str;
return result;
}
private String getPath()
{
URL url = getClass().getResource(getClass().getName() + ".class");
String classPath = url.getHost() + url.getFile();
int pos = classPath.lastIndexOf("/");
return classPath.substring(0, pos + 1);
}
// 读取文件
private byte[] readFile(String filename)
{
int fileSize = 0;
RandomAccessFile fileH = null;
// Opens the file for reading.
try
{
fileH = new RandomAccessFile(filename, "r");
fileSize = (int) fileH.length();
} catch (IOException ioErr)
{
System.err.println("Cannot find " + filename);
System.err.println(ioErr);
System.exit(200);
}
// allocates the buffer large enough to hold entire file
byte[] buf = new byte[fileSize];
// reads all bytes of file
int i = 0;
try
{
while (true)
{
try
{
buf[i++] = fileH.readByte();
} catch (EOFException e)
{
break;
}
}
} catch (IOException ioErr)
{
System.out.println("ERROR in reading of file" + filename);
}
return buf;
}
public static void main(String[] args)
{
SeedAudit seed = new SeedAudit("e:/Sample.mms");
}
}
Java 读取文件
原创
©著作权归作者所有:来自51CTO博客作者JavaAlpha的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Freemark 常用代码总结1
下一篇:跆拳道反应速度的训练方法(一)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java 读取 Excel 文件(XLS/XLSX)完整指南
对于需要处理电子表格数据的Java开发者而言,Spire.XLS for Java 库提供了一个强大且高效的解决方案。本完整指南将详细介绍如何使用该库读取XLS和XLSX格式的Excel文件,包括提取单元格数据、公式计算结果以及图像等内容,帮助您快速实现Excel文档的自动化处理与集成。
Java API Spire.XLS for Java Java Excel Reading XLS/XLSX Processing -
java cvs文件读取 java 读取wav文件
我想读一个音频文件,它从22050赫兹降到8000赫兹。对于22050赫兹,缓冲区大小为512。有没有解决这个问题或其他方法来读取Java中的下采样?wav文件?使用readexample类从
java cvs文件读取 java wav压缩 java Code System -
java 读取文件地址 java读取文件信息
1、按字节读取文件内容2、按字符读取文件内容3、按行读取文件内容4、随机读取文件内容
java 读取文件地址 java对文件读取 System 读取文件 字节数 -
JAVA 读取 指定文件 java读取文件内容
java读取文件内容的方法总结 经常用到Java读取文件,做了个总结,分以下几种: 1,以字节为单位读取文件; 2,以字符为单位读取文件; 3,以行为单位读取文件; 4,随机读取
JAVA 读取 指定文件 java 读取文件 System
















