- public class ServerWindow extends Frame
- {
- private FileServer s = new FileServer(7878);
- private Label label;
- public ServerWindow(String title)
- {
- super(title);
- label = new Label();
- add(label, BorderLayout.PAGE_START);
- label.setText("服务器已经启动");
- this.addWindowListener(new WindowListener()
- {
- @Override
- public void windowOpened(WindowEvent e)
- {
- new Thread(new Runnable()
- {
- @Override
- public void run()
- {
- try
- {
- s.start();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }).start();
- }
- @Override
- public void windowIconified(WindowEvent e) {
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- }
- @Override
- public void windowClosing(WindowEvent e) {
- s.quit();
- System.exit(0);
- }
- @Override
- public void windowClosed(WindowEvent e) {
- }
- @Override
- public void windowActivated(WindowEvent e) {
- }
- });
- }
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- ServerWindow window = new ServerWindow("文件上传服务端");
- window.setSize(300, 300);
- window.setVisible(true);
- }
- }
- public class SocketClient
- {
- public static void main(String[] args)
- {
- try
- {
- //这里的套接字根据实际服务器更改
- Socket socket = new Socket("127.0.0.1", 7878);
- OutputStream outStream = socket.getOutputStream();
- String filename = "QQWubiSetup.exe";
- File file = new File(filename);
- //构造上传文件头,上传的时候会判断上传的文件是否存在,是否存在上传记录
- //若是不存在则服务器会自动生成一个id,给客户端返回
- String head = "Content-Length="+ file.length() + ";filename="+ filename + ";sourceid=1278916111468/r/n";
- outStream.write(head.getBytes());
- PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
- String response = StreamTool.readLine(inStream);
- System.out.println(response);
- String[] items = response.split(";");
- //构造开始上传文件的位置
- String position = items[1].substring(items[1].indexOf("=")+1);
- //以读的方式开始访问
- RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
- fileOutStream.seek(Integer.valueOf(position));
- byte[] buffer = new byte[1024];
- int len = -1;
- int i = 0;
- while( (len = fileOutStream.read(buffer)) != -1)
- {
- outStream.write(buffer, 0, len);
- i++;
- //if(i==10) break;
- }
- fileOutStream.close();
- outStream.close();
- inStream.close();
- socket.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- * 读取流
- * @param inStream
- * @return 字节数组
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws Exception
- {
- ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = -1;
- while( (len=inStream.read(buffer)) != -1)
- {
- outSteam.write(buffer, 0, len);
- }
- outSteam.close();
- inStream.close();
- return outSteam.toByteArray();
- }
- }
- public class StreamTool
- {
- public static void save(File file, byte[] data) throws Exception
- {
- FileOutputStream outStream = new FileOutputStream(file);
- outStream.write(data);
- outStream.close();
- }
- public static String readLine(PushbackInputStream in) throws IOException
- {
- char buf[] = new char[128];
- int room = buf.length;
- int offset = 0;
- int c;
- loop: while (true) {
- switch (c = in.read())
- {
- case -1:
- case '/n':
- break loop;
- case '/r':
- int c2 = in.read();
- if ((c2 != '/n') && (c2 != -1)) in.unread(c2);
- break loop;
- default:
- if (--room < 0) {
- char[] lineBuffer = buf;
- buf = new char[offset + 128];
- room = buf.length - offset - 1;
- System.arraycopy(lineBuffer, 0, buf, 0, offset);
- }
- buf[offset++] = (char) c;
- break;
- }
- }
- if ((c == -1) && (offset == 0)) return null;
- return String.copyValueOf(buf, 0, offset);
- }
- /**
- * 读取流
- * @param inStream
- * @return 字节数组
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws Exception
- {
- ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = -1;
- while( (len=inStream.read(buffer)) != -1){
- outSteam.write(buffer, 0, len);
- }
- outSteam.close();
- inStream.close();
- return outSteam.toByteArray();
- }
- }