2018-07-21


考试题1

  • 针对pdf打不开的问题进行改正:

    public class App
    {
    public static void main( String[] args )
    {
        try {
            // 实际不确定文件类型的时候,需要用字节流进行传递,之后会原样输出,否则会有错误
            URL url = new URL("http://192.168.11.205:18080/trainning/SampleChapter1.pdf");
            URLConnection connection = url.openConnection();
    
            InputStream is = connection.getInputStream();
    
            byte [] getData = readInputStream(is);
    
            File f1 = new File("Exam1/tmp");
            if(!f1.exists()) {
                f1.mkdir();
            }
    
            File file = new File("Exam1/tmp/SampleChapter1.pdf");
    
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(getData);
            if (fos!=null){
                fos.close();
            }
            if (is != null){
                is.close();
            }
    
    
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte [] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer))!=-1){
            bos.write(buffer,0,len);
        }
        bos.close();
        return bos.toByteArray();
    
    }
    }
    

考试题2

  • 针对pdf打不开的问题进行改正
  • Server端

    public class Server {
    public static void main(String [] arga){
    
    
        try{
            ServerSocket ss = new ServerSocket(12345);
            Socket s = ss.accept();
            System.out.println("connect!");
    
            File file = new File("Exam1/tmp/SampleChapter1.pdf");
    
            FileInputStream fis = new FileInputStream(file);
    
            byte b [] = new byte[1024];
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
    
            int len;
            while ((len = fis.read(b))!= -1){
                baos.write(b, 0 ,len);
    
            }
            bos.write(baos.toByteArray());
    
            bos.close();
            baos.close();
            fis.close();
    
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("程序运行错误:"+e);
        }
    }
    }
    
  • Client端

    public class Client {
    public static void main(String [] arga){
        try {
            Socket s = new Socket("127.0.0.1",12345);
    
            InputStream is = s.getInputStream();
    
            File f1 = new File("Exam2/tmp");
            if (!f1.exists()){
                f1.mkdir();
            }
            File file = new File("Exam2/tmp/SampleChapter1.pdf");
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    
            byte b [] = new byte[1024];
            int len = 0;
            while ((len = is.read(b))!= -1){
                baos.write(b, 0 ,len);
            }
            bos.write(baos.toByteArray());
    
            bos.close();
            baos.close();
            is.close();
    
    
        }catch(ConnectException connExc){
            System.out.println("连接失败!");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }
    }
    }