android利用Serialization向服务器端发送任意的文件(一)
1、?先创建一个Java实现了Serializable接口的类FilePojo,代码如下:
?
//?必须实现Serializable接口。否则无法调用ObjectOutputStream的
// writeObject方法,或者ObjectInputStream中的readObject方法
public?class?FilePojo?implements?Serializable
{
?????????private?static?final?long?serialVersionUID?= 1L;
?????????private?String?fileName;????????????//?文件名称
?????????private?long?fileLength;?????????????//?文件长度
?????????private?byte[]?fileContent;??????????//?文件内容
?????????public?String getFileName()
?????????{
???????????????????return?fileName;
?????????}
?????????public?void?setFileName(String fileName)
?????????{
???????????????????this.fileName?= fileName;
?????????}
??????? ? ??public?long?getFileLength()
?????????{
???????????????????return?fileLength;
?????????}
?????????public?void?setFileLength(long?fileLength)
?????????{
???????????????????this.fileLength?= fileLength;
?????????}
?????????public?byte[] getFileContent()
?????????{
???????????????????return?fileContent;
?????????}
?????????public?void?setFileContent(byte[] fileContent)
?????????{
???????????????????this.fileContent?= fileContent;
??????? }
}
?
这个类将在服务器端和客户端都需要也会被用到。
2、编写服务端代码如下:
public?class?ObjectServer
{
?????????public?static?void?main(String[] args)?throws?ClassNotFoundException
?????????{
???????????????????ServerSocket serverSocket;
???????????????????FileOutputStream fos;
???????????????????try
???????????????????{
????????????????????????????serverSocket =?new?ServerSocket(433);
????????????????????????????while(true)
????????????????????????????{
??????????????????????????????????? ??Socket clientSocket = serverSocket.accept();
???? ? ?System.out.println("socket open");
?????????????????????????????????????//?从clientSocket获取ObjectInputStream对象
?????????????????????????????????????ObjectInputStream ois =?new?ObjectInputStream(clientSocket.getInputStream());
?????????????????????????????????????//?读取从客户端传递过来的FilePojo对象
?????????????????????????????????????FilePojo fpo = (FilePojo) ois.readObject();
?????????????????????????????????????System.out.println(fpo.getFileName());
?????????????????????????????????????System.out.println(fpo.getFileLength());
?????????????????????????????????????//?初始化FileOutputStream对象fos
?????????????????????????????????????fos =?new?FileOutputStream("D:\\"?+ fpo.getFileName());
?????????????????????????????????????//?将fpo中的内容写入fpo
?????????????????????????????????????fos.write(fpo.getFileContent(), 0, (int)fpo.getFileLength());
?????????????????????????????????????fos.close();
?????????????????????????????????????ois.close();
????????????????????????????}
???????????????????}
???????????????????catch(IOException ioe)
???????????????????{
?? ? ? ? ? ? ? ? ? ? ? ? ???System.out.println("socket error and closed");
???????????????????}
?????????}
}
?
3、编写Android客户端端代码如下:
显示layout文件:main.xml
?
<?xml?version="1.0"?encoding="utf-8"?>
<LinearLayout?
?? ?xmlns:android="http://schemas.android.com/apk/res/android"
????android:orientation="vertical"
????android:layout_width="fill_parent"
????android:layout_height="fill_parent"?>
?????????<Button?android:id="@+id/btn1"
???????????????????android:layout_width="fill_parent"
???????????????????android:layout_height="wrap_content"
???????????????????android:text="发送图片">
?
?????????</Button>
</LinearLayout>
再是Activty代码
接android利用Serialization向服务器端发送任意的文件(二)