- package test;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class TCPUploadClientPic {
- /*
- * 上传图片
- */
- public static void main(String[] args) throws UnknownHostException, IOException {
- // TODO Auto-generated method stub
- //socket服务
- Socket socket=new Socket("pcx",4444);
- OutputStream outputStream=socket.getOutputStream();
- //字节输入流,读取一个图片文件
- FileInputStream fileInputStream=new FileInputStream("d:\\2.JPG");
-
- int len=0;
- byte[] buf=new byte[1024];
- while((len=fileInputStream.read(buf))!=-1){
- outputStream.write(buf,0,len);
- }
- socket.shutdownOutput();
- BufferedReader ServersReturn=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- //显示服务器返回的信息
- System.out.println(" 服务器信息:"+ServersReturn.readLine());
- outputStream.close();
- ServersReturn.close();
- socket.close();
- }
- }
复制代码- package test;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TCPUploadServersPic {
- /*
- *上传图片的服务器端
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- ServerSocket serverSocket=new ServerSocket(4444);
- Socket socket =serverSocket.accept();
- File file=new File("d:\\upload");
- if(!file.exists())
- file.mkdirs();
- InputStream inputStream=socket.getInputStream();
- FileOutputStream fileOutputStream= new FileOutputStream(file+File.separator+System.currentTimeMillis()+".jpg");
- int len=0;
- byte[] buf=new byte[1024];
- while((len=inputStream.read())!=-1){
- fileOutputStream.write(buf,0,len);
- }
- OutputStream outputStream=socket.getOutputStream();
- outputStream.write("上传成功".getBytes());
- fileOutputStream.close();
- socket.close();
- serverSocket.close();
- }
- }
复制代码 如题~~,亲,帮解决一下
|
|