由于项目的需要,需要写一个解析文件的类。标签:文件,saveToFile,读取,else,content,getContentId,Java,mmContent,String From: https://blog.51cto.com/javaalpha/5893248
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");
}
}