java socket数据上传.doc_第1页
java socket数据上传.doc_第2页
java socket数据上传.doc_第3页
java socket数据上传.doc_第4页
java socket数据上传.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

主要功能:(1)有客户端和服务器端(2)客户端可以上传各种类型的文件(3)服务器端可以接受很多文件上传(4)客户端发出请求后,在服务器端应答后能将文件传到服务器上(5)实验实现时用环回测试将本机看作客户端和服务器端Client.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import .*;import java.io.*;public class Client extends JFrame implements ActionListener String str=; private JTextField t1; private JLabel l1,l2; private JButton Exit,Ok,Open; private JFileChooser choose=new JFileChooser(); private File Myfile; public Client() Container c=getContentPane(); Panel p1=new Panel(); Panel p2=new Panel(); Panel p3=new Panel(); c.setLayout(new GridLayout(3,1); p1.setLayout(new FlowLayout(); p2.setLayout(new FlowLayout(); p3.setLayout(new GridLayout(2,1); t1=new JTextField(35); l1=new JLabel(,JLabel.CENTER); l2=new JLabel(,JLabel.CENTER); Exit=new JButton(退出); Ok=new JButton(上传); Open=new JButton(浏览.); Exit.addActionListener(this); Ok.addActionListener(this); Open.addActionListener(this); p1.add(t1); p1.add(Open); c.add(p1); p2.add(Ok); p2.add(Exit); c.add(p2); p3.add(l1); p3.add(l2); c.add(p3); setSize(500,200); show(); public void actionPerformed(ActionEvent e) str=; if(e.getSource()=Exit)System.exit(0); try if(e.getSource()=Ok) if(t1.getText()!=) this.client(); catch(Exception t) System.out.println(t.toString(); if(e.getSource()=Open) this.openf(); public void openf() int result=choose.showOpenDialog(null); Myfile=choose.getSelectedFile(); if(result=JFileChooser.APPROVE_OPTION) t1.setText(Myfile.getPath(); else if(result=JFileChooser.CANCEL_OPTION) t1.setText(); Myfile=null; public void client() byte ff; int size; Socket clientSocket; PrintStream ps; String fileName; try /创建一个上传的Socket clientSocket=new Socket(,8765); ps=new PrintStream(clientSocket.getOutputStream(); size=(int)Myfile.length(); fileName=Myfile.getName(); BufferedReader br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream(); String over=n; byte p=over.getBytes(); byte name=(fileName+over).getBytes(); System.out.println(new String(name); /文件名 ps.write(name); ps.flush(); String s1=br.readLine(); String tmp=String.valueOf(size)+over; byte bsize=tmp.getBytes(); /文件大小 ps.write(bsize); ps.flush(); String s2=br.readLine(); str=上传结束,请选择其它文件; l1.setText(str); str=已上传的文件为+s1.trim()+;大小为+s2.trim(); l2.setText(str); /文件内容 FileInputStream in=new FileInputStream(Myfile); BufferedInputStream bf=new BufferedInputStream(in); ff=new byte1024; int c; while(c=bf.read(ff)!=-1) ps.write(ff,0,c); ps.close(); bf.close(); in.close(); clientSocket.close(); t1.setText(); catch(Exception e) System.out.println(e.toString(); public static void main(String args) Client app=new Client(); app.addWindowListener(new WindowAdapter() public void WindowClosing(WindowEvent e) System.exit(0); ); Server.javaimport .*;import java.io.*;import java.util.*;class MyThread extends Threadprivate Socket clientSocket;private int counter;public MyThread(Socket i,int c)clientSocket=i;counter=c;/采用同步机制public synchronized void run()tryDataInputStream br=new DataInputStream(clientSocket.getInputStream();DataOutputStream output=new DataOutputStream(clientSocket.getOutputStream();/文件大小byte n=new byte225;br.read(n);output.write(n);output.flush();/文件名byte ll=new byte255;br.read(ll);output.write(ll);output.flush();String tmpe=new String(ll);int a=Integer.parseInt(tmpe.trim();/创建文件用于保存上传的文件String names=new String(n);File Myfile=new File(d:);String files=d:+Myfile.separator+(names.trim();FileOutputStream out=new FileOutputStream(files);BufferedOutputStream bw=new BufferedOutputStream(out);byte ff=new byte1024;int c=0;/把上传的数据输出至文件while(c=br.read(ff)!=-1)bw.write(ff,0,c);bw.close();output.close();out.close();br.close();clientSocket.close();catch(Exception e)System.out.println(e);public class Serverprivate static Socket clientSocket;private static int counter;public static void main(String args)int i=1;ServerSocket serverSocket;tryserverSocket=new ServerSocket(8765);while(true) System.out.println(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论