socktet传输图片和文字

来源:互联网 发布:海闻教授知乎 编辑:程序博客网 时间:2024/05/10 09:58

server:

package com.njtd.webservice;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import com.njtd.frame.util.SystemConfigUtil;public class ScoketServer {public static void main(String[] args) {ScoketServer ss = new ScoketServer();ss.server();}public void server(){try {ServerSocket serverSocket = new ServerSocket(8866);Socket socket = serverSocket.accept();DataOutputStream dos = new DataOutputStream(socket.getOutputStream());DataInputStream dis = new DataInputStream(socket.getInputStream());int message = dis.readInt();if(1 == message){//uploadupload(dis);}else if(2 == message){//downloaddownload("",dos);}} catch (Exception e) {e.printStackTrace();}}public void upload(DataInputStream dis){StringBuffer path = new StringBuffer();//String url = SystemConfigUtil.getInstance().get("path_upload_root");//String url = "E:/tt";path.append(url + "/facadeImages"); // 服务器保存图片目录File file = new File(path.toString());if (!file.exists()) // 如果目录不存在就创建file.mkdirs();Date date = new Date();DateFormat df = new SimpleDateFormat("yyyyMMddHHmmsss");path.append("/");path.append(df.format(date.getTime()));path.append(".jpg");try{FileOutputStream fos = new FileOutputStream(path.toString());byte[] buffer = new byte[1024];int len;while (((len = dis.read(buffer)) > 0))fos.write(buffer, 0, len);dis.close();fos.flush();//fos.close();}catch (Exception e) {e.printStackTrace();}}public void download(String picPath,DataOutputStream dos){File file = new File(picPath);try{if (file.exists()){ // 如果文件不存在就创建dos.writeUTF("true");FileInputStream fis = new FileInputStream(picPath);byte[] buffer = new byte[1024];int len;while (((len = fis.read(buffer)) > 0))dos.write(buffer, 0, len);fis.close();dos.flush();//dos.close();}else{dos.writeUTF("false");dos.flush();//dos.close();}}catch (Exception e) {e.printStackTrace();}}}


client:

package com.njtd.webservice;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import com.njtd.frame.util.SystemConfigUtil;public class ScoketClient {public static void main(String[] args) {ScoketClient ss = new ScoketClient();ss.server();}public void server(){try {Socket clientSocket = new Socket(InetAddress.getLocalHost(), 8866);DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());DataInputStream dis = new DataInputStream(clientSocket.getInputStream());dos.writeInt(1);//1:upload,2:downloadupload("F:/2.jpg",dos);/*dos.writeUTF("F:/2.jpg");download(dis);*/} catch (Exception e) {e.printStackTrace();}}public void download(DataInputStream dis){StringBuffer path = new StringBuffer();//String url = SystemConfigUtil.getInstance().get("path_upload_root");//String url = "E:/tt2";path.append(url + "/facadeImages"); // 服务器保存图片目录File file = new File(path.toString());if (!file.exists()) // 如果目录不存在就创建file.mkdirs();Date date = new Date();DateFormat df = new SimpleDateFormat("yyyyMMddHHmmsss");path.append("/");path.append(df.format(date.getTime()));path.append(".jpg");try{FileOutputStream fos = new FileOutputStream(path.toString());byte[] buffer = new byte[1024];int len;while (((len = dis.read(buffer)) > 0))fos.write(buffer, 0, len);dis.close();fos.flush();fos.close();}catch (Exception e) {e.printStackTrace();}}public void upload(String picPath,DataOutputStream dos){try{FileInputStream fis = new FileInputStream(picPath);byte[] buffer = new byte[1024];int len;while (((len = fis.read(buffer)) > 0))dos.write(buffer, 0, len);fis.close();dos.flush();dos.close();}catch (Exception e) {e.printStackTrace();}}}


 

0 0