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向服务器端发送任意的文件(二)
分享到:
相关推荐
Kotlin cross-platform / multi-format reflectionless serialization Kotlin serialization consists of a compiler plugin, which automatically produces visitor code for classes, and runtime library, ...
sirenix.serialization.dll
System.Runtime.Serialization.DLL
kotlinx-serialization-compiler-plugin.jar
资源来自pypi官网。 资源全名:oslo.serialization-2.2.0.tar.gz
System.Runtime.Serialization.dll System.Runtime.Serialization.dll System.Runtime.Serialization.dll
JAX-RS Kotlinx Serialization A message body reader/writer and parameter converter which uses kotlinx-serialization. Usage val resourceConfig = new ResourceConfig(); resourceConfig.register(Json....
hystrix-serialization-1.5.18.jar
- 整个代码由一个头文件组成 json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来非常方便 - 语法直观,就像写普通的c++代码 - 不止用起来似c++习惯和风格,更是使用 C++ 11 标准编写 - 使用 ...
数据的序列化和反序列化 Serialization DeSerialization
akka-kryo-serialization, 基于Kryo的Akka序列化 akka-kryo-serialization-- Scala 和Akka基于kryo的序列化程序这个库为 Scala 和Akka提供定制的基于kryo的序列化程序。 它可以用于更高效的akka远程处理。它还可以...
.net2.0版本的json操作类 ...System.Web.Script.Serialization的json操作类,位于3.0以上的System.Web.Extensions类库中. 本项目是从System.Web.Extensions中剥离出System.Web.Script.Serialization的完整项目
word\Excel文件等),这里只提供一个简单的文本文件,应用该文件我做出了把任何文件存储为二进制,而读取存储的二进制文件时报了一异常信息:异常类型: System.Runtime.Serialization.Serialization 异常消息: 二...
此压缩包包含12个项目,每个项目都着重讲解Boost::Serialization一个方面的使用技巧,有详细的代码介绍具体使用方法。
it also makes (de)serialization painless. It wraps around the well-known org.json classes (JSONObject, JSONArray, etc.) which also happen to be included in the Android SDK. As we all know, those ...
Paper is a fast NoSQL data storage for Android that lets you save/restore Java/Kotlin objects using efficient Kryo serialization. Class structure changes handled automatically. What's new in 2.1 Get...
该库向kotlinx.serialization添加了YAML支持。 支持YAML 1.2版。 使用示例从YAML解析到Kotlin对象@Serializable数据类Team(val领导者:字符串,val成员:列表 )val input =“”“领导者:艾米(Amy)成员:-鲍勃-...
注意:JsonHelper需要引用System.Runtime.Serialization;需要有xml文件记录token和expiretime;只有发送消息;接收消息待上传。。。