• 上传文件 服务端模拟存储


    这里可以选用Parcelable来进行序列化,parcelable效率更高,我这里选用的是serializable

    服务端和客户端都要有此文件,并且所在的包名要一致

    不懂可以参考parcelable(http://www.cnblogs.com/mydomainlistentome/p/4687173.html);

    package lyl.sole.util;
    
    import java.io.Serializable;
    
    public class SerializUtil implements Serializable {
    	private String title;  
    	private byte[] contentData;
    	private long contentLength;
    	private String ext;
    	private String TypeMime;
    
    	
    	public String getTitle() {
    		return title;
    	}
    	public void setTitle(String title) {
    		this.title = title;
    	}
    	public byte[] getContentData() {
    		return contentData;
    	}
    	public void setContentData(byte[] contentData) {
    		this.contentData = contentData;
    	}
    	public long getContentLength() {
    		return contentLength;
    	}
    	public void setContentLength(long contentLength) {
    		this.contentLength = contentLength;
    	}
    	public String getExt() {
    		return ext;
    	}
    	public void setExt(String ext) {
    		this.ext = ext;
    	}
    	public String getTypeMime() {
    		return TypeMime;
    	}
    	public void setTypeMime(String typeMime) {
    		TypeMime = typeMime;
    	}
    	
    	
    }

    客户端

    package com.example.webfile;
    
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import lyl.sole.util.SerializUtil;
    
    import com.example.webfile.R.id;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.Handler;
    import android.os.Message;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    	private Button send;
    	private TextView show;
    	private static final int FINISH = 0 ;
    //在主线程中更新UI private Handler handle = new Handler() { public void handleMessage(Message msg) { switch(msg.what) { case FINISH: String result = msg.obj.toString() ; // 取出数据 if ("true".equals(result)) { show.setText("操作成功!"); } else { show.setText("操作失败!"); } break ; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); send = (Button) findViewById(id.send); show = (TextView) findViewById(id.show); send.setOnClickListener(new sendFile()); } private class sendFile implements OnClickListener { public void onClick(View arg0) { Thread thread = new Thread(null, inThreadOn, "sendfile"); //在子线程中更新执行操作
                   thread.start(); } } private SerializUtil parcelFile() { SerializUtil serializ = new SerializUtil(); serializ.setTitle("小胡子"); serializ.setTypeMime("image/jpeg"); File file = new File(Environment.getExternalStorageDirectory() .toString() + File.separator + "head_8.png"); InputStream input = null; try {
                  //读入图片 input = new FileInputStream(file); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int leng = 0;
                //读入字节数组 while ((leng = input.read(data)) != -1) {
                //读出 output.write(data, 0, leng);    }
                  //资源序列化
        			serializ.setContentData(output.toByteArray());
    			serializ.setContentLength(file.length());
    			serializ.setExt("png");
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				input.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return serializ;
    
    	}
    
    	public Runnable inThreadOn = new Runnable() {
    		public void run() {
    //在子线程中执行网络操作 upLoadFile(); } }; private void upLoadFile() { try { final Socket client = new Socket("192.168.156.1", 8888); // 读取返回数据 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(client.getInputStream())); //想服务端写数据
                   ObjectOutputStream oos = new ObjectOutputStream( client.getOutputStream()); SerializUtil getdata = parcelFile(); oos.writeObject(getdata); String result = null; result = bufferedReader.readLine(); oos.close();
                //传message通知主线程跟新UI // Message msg=Message.obtain(); // msg.obj=result; // msg.sendToTarget(); Message msg=handle.obtainMessage(FINISH,result); msg.sendToTarget(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }

    服务端

    import java.net.ServerSocket;
    
    
    public class Service {
    
    	public static void main(String[] args) throws Exception {
    		ServerSocket server=new ServerSocket(8888);
    		boolean flag=true;
    		while(flag){
    			// 启动线程
    			new Thread(new ServiceUtil(server.accept())).start();
    		}
    		// 关闭
    		server.close();
    	}
    
    }
    

    服务端存储

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.util.UUID;
    
    import lyl.sole.util.SerializUtil;
    
    public class ServiceUtil implements Runnable {
    	// 存储路径
    	private static final String DIRPATH = "D:" + File.separator + "android"
    			+ File.separator;
    	private Socket client = null;
    	private SerializUtil serializ = null;
    	private int i = 0;
    
    	public ServiceUtil(Socket client) {
    		this.client = client;
    		System.out.println("客户端连接" + i++);
    	}
    
    	@Override
    	public void run() {
    		PrintStream out;
    		try {
    			out = new PrintStream(client.getOutputStream());
    			// 反序列化
    			ObjectInputStream in = new ObjectInputStream(
    					client.getInputStream());
    			serializ = (SerializUtil) in.readObject();
    			System.out.println("标题" + serializ.getTitle());
    			System.out.println("类型" + serializ.getTypeMime());
    			System.out.println("大小" + serializ.getContentLength());
    			out.print(saveFile());
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				this.client.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    
    	}
    	private boolean saveFile() throws Exception { // 负责文件内容的保存
    		//UUID 通用唯一标识符
    		File file=new File(DIRPATH+UUID.randomUUID()+"."+this.serializ.getExt());
    		if(!file.getParentFile().exists()){
    			file.getParentFile().mkdir();
    		}
    		OutputStream output=null;
    		output=new FileOutputStream(file);
    		output.write(this.serializ.getContentData());
    			return true;
    	}
    }
    

      权限、布局 就不粘了。

     

  • 相关阅读:
    asp.net如何实现删除文件的操作? (转)
    开始计算机英语的学习,先把这里当生词本用了。
    CSS Box Model 盒子模型
    生成网站快捷方式
    Microsoft .NET Framework 3.5 sp1离线安装解决方案
    asp.net生成网站快捷方式
    https://mail.google.com/tasks/ig?pli=1
    Sql server DATEDIFF DATEADD
    局域网 跨数据库 访问数据库
    asp.net 生成网站快捷方式
  • 原文地址:https://www.cnblogs.com/mydomainlistentome/p/4705254.html
Copyright © 2020-2023  润新知