思想:
1直接将所有数据安装字节数组发送
2对象序列化方式
/
/
thread方式
@author Administrator
/
public class TestSocketActivity4 extends Activity {
private static final int FINISH = 0;
private Button send = null;
private TextView info = null;
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msgwhat) {
case FINISH:
String result = msgobjtoString(); // 取出数据
if ("true"equals(result)) {
TestSocketActivity4thisinfosetText(" *** 作成功!");
} else {
TestSocketActivity4thisinfosetText(" *** 作失败!");
}
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
supersetContentView(Rlayoutactivity_test_sokect_activity4);
// StrictModesetThreadPolicy(new StrictModeThreadPolicyBuilder()
// detectDiskReads()detectDiskWrites()detectNetwork()
// penaltyLog()build());
// StrictModesetVmPolicy(new StrictModeVmPolicyBuilder()
// detectLeakedSqlLiteObjects()detectLeakedClosableObjects()
// penaltyLog()penaltyDeath()build());
thissend = (Button) superfindViewById(Ridsend);
thisinfo = (TextView) superfindViewById(Ridinfo);
thissendsetOnClickListener(new SendOnClickListener());
}
private class SendOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
try {
new Thread(new Runnable() {
@Override
public void run() {
try {
//1:
Socket client = new Socket("1921681165", 9898);
//2:
ObjectOutputStream oos = new ObjectOutputStream(
clientgetOutputStream());
//3:
UploadFile myFile = SendOnClickListenerthis
getUploadFile();
//4:
ooswriteObject(myFile);// 写文件对象
// ooswriteObject(null);// 避免EOFException
oosclose();
BufferedReader buf = new BufferedReader(
new InputStreamReader(client
getInputStream())); // 读取返回的数据
String str = bufreadLine(); // 读取数据
Message msg = TestSocketActivity4thismyHandler
obtainMessage(FINISH, str);
TestSocketActivity4thismyHandlersendMessage(msg);
bufclose();
clientclose();
} catch (Exception e) {
Logi("UploadFile", egetMessage());
}
}
})start();
} catch (Exception e) {
eprintStackTrace();
}
}
private UploadFile getUploadFile() throws Exception { // 包装了传送数据
UploadFile myFile = new UploadFile();
myFilesetTitle("tangcco安卓之Socket的通信"); // 设置标题
myFilesetMimeType("image/png"); // 的类型
File file = new File(EnvironmentgetExternalStorageDirectory()
toString()
+ Fileseparator
+ "Pictures"
+ Fileseparator
+ "bpng");
InputStream input = null;
try {
input = new FileInputStream(file); // 从文件中读取
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte data[] = new byte[1024];
int len = 0;
while ((len = inputread(data)) != -1) {
boswrite(data, 0, len);
}
myFilesetContentData(bostoByteArray());
myFilesetContentLength(filelength());
myFilesetExt("png");
} catch (Exception e) {
throw e;
} finally {
inputclose();
}
return myFile;
}
}
}
public class UploadFile implements Serializable {
private String title;
private byte[] contentData;
private String mimeType;
private long contentLength;
private String ext;
public String getTitle() {
return title;
}
public void setTitle(String title) {
thistitle = title;
}
public byte[] getContentData() {
return contentData;
}
public void setContentData(byte[] contentData) {
thiscontentData = contentData;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
thismimeType = mimeType;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
thiscontentLength = contentLength;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
thisext = ext;
}
}
下边是服务端
public class Main4 {public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(9898); // 服务器端端口
Systemoutprintln("服务启动");
boolean flag = true; // 定义标记,可以一直死循环
while (flag) { // 通过标记判断循环
new Thread(new ServerThreadUtil(serveraccept()))start(); // 启动线程
}
serverclose(); // 关闭服务器
}
}
public class ServerThreadUtil implements Runnable {
private static final String DIRPATH = "D:" + Fileseparator + "myfile"
+ Fileseparator; // 目录路径
private Socket client = null;
private UploadFile upload = null;
public ServerThreadUtil(Socket client) {
thisclient = client;
Systemoutprintln("新的客户端连接");
}
@Override
public void run() {
try {
ObjectInputStream ois = new ObjectInputStream(
clientgetInputStream()); // 反序列化
thisupload = (UploadFile) oisreadObject(); // 读取对象//UploadFile需要和客户端传递过来的包名类名相同,如果不同则会报异常
Systemoutprintln("文件标题:" + thisuploadgetTitle());
Systemoutprintln("文件类型:" + thisuploadgetMimeType());
Systemoutprintln("文件大小:" + thisuploadgetContentLength());
PrintStream out = new PrintStream(thisclientgetOutputStream());// BufferedWriter
outprint(thissaveFile());//返回响应
// BufferedWriter writer = null;
// writerwrite("");
} catch (Exception e) {
eprintStackTrace();
} finally {
try {
thisclientclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
private boolean saveFile() throws Exception { // 负责文件内容的保存
/
javautilUUIDrandomUUID():
UUIDrandomUUID()toString()是javaJDK提供的一个自动生成主键的方法。 UUID(Universally
Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的,
是由一个十六位的数字组成
,表现出来的形式。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,
过几秒又生成一个UUID,
则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得
),UUID的唯一缺陷在于生成的结果串会比较长,字符串长度为36。
UUIDrandomUUID()toString()是java JDK提供的一个自动生成主键的方法。 UUID(Universally
Unique Identifier)全局唯一标识符, 是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的,
是由一个十六位的数字组成,表现出来的形式
/
File file = new File(DIRPATH + UUIDrandomUUID() + ""
+ thisuploadgetExt());
if (!filegetParentFile()exists()) {
filegetParentFile()mkdir();
}
OutputStream output = null;
try {
output = new FileOutputStream(file);
outputwrite(thisuploadgetContentData());
return true;
} catch (Exception e) {
throw e;
} finally {
outputclose();
}
}
}
public class UploadFile implements Serializable {
private String title;
private byte[] contentData;
private String mimeType;
private long contentLength;
private String ext;
public String getTitle() {
return title;
}
public void setTitle(String title) {
thistitle = title;
}
public byte[] getContentData() {
return contentData;
}
public void setContentData(byte[] contentData) {
thiscontentData = contentData;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
thismimeType = mimeType;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
thiscontentLength = contentLength;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
thisext = ext;
}
}1、正版软件一般到官网下载。AE是Adobe公司出品,所以可以到Adobe公司官方网站下载。同样,会声会影版权属于Corel Corporation中国,到官方网站可下载最新版本。
2、由于某些官方网站提供下载的服务器在国外,下载软体速度较慢,建议也可以到国内正规、大型的软件站下载,比如华军软件站。
3、正版软件需要购买注册,但是安全性及后期维护升级方便的多。支持正版,从你做起。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)