首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个DataInput/Output流混淆

多个DataInput/Output流混淆
EN

Stack Overflow用户
提问于 2013-02-07 21:24:29
回答 1查看 342关注 0票数 1

我正在创建一个简单的程序,允许上传和下载到文件服务器。在服务器完全在线之前,我需要在上传和下载之间做出选择。问题是,我真的对DataInput/Output streams等感到困惑,目前上传选项不起作用。它将自己工作,但不是以我在这里编码的方式。在这个阶段我的头都要碎了!

服务器代码:

代码语言:javascript
复制
import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;

 public class Server_Test {

public static void main(String a[]) throws Exception {

    int timeoutsecs = 600;
    int port = 4444;
    Socket sock;
    ServerSocket servsock = new ServerSocket(port, timeoutsecs);

    while (true) {

        // wait for the next client connection
        sock = servsock.accept();

        DataInputStream choice = new DataInputStream(sock.getInputStream());


        if (choice.read() == 1){

        // ****1*****
        // Download
        // Send I/O streams to the socket



        DataOutputStream out = new DataOutputStream(sock.getOutputStream());
        new Server_Test().sendFile(out);

        }else if (choice.read() == 2){

        // ****2****
        //Upload
        // Receive I/O streams from socket



        DataInputStream in = new DataInputStream(sock.getInputStream());
        new Server_Test().receiveFile(in);

        }

        // Close this connection, (not the overall // server socket)

        sock.close(); 

}
}

public void sendFile(DataOutputStream os) throws Exception{

     // String fileLoc = JOptionPane.showInputDialog(null, "What is the file location of the file you want to download?");
    File file = new File("C:\\TestFile.txt");
    FileInputStream fis = new FileInputStream(file);
    byte [] mybytearray  = new byte [(int)file.length()+1];

    BufferedInputStream bis = new BufferedInputStream(fis);

      bis.read(mybytearray,0,mybytearray.length);
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();

    bis.close();


}

public void receiveFile(DataInputStream in) throws Exception{

    int filesize=6022386;
      int bytesRead;
      int current = 0;
      byte [] mybytearray  = new byte [filesize];

        FileOutputStream fos = new FileOutputStream("C:\\UploaTest.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = in.read(mybytearray,0,mybytearray.length);
        current = bytesRead;


        do {
           bytesRead =
              in.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);

        bos.write(mybytearray, 0 , current);
        bos.flush();
        bos.close();

}

}

客户端代码:

代码语言:javascript
复制
import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;

public class Client_Test {
public static void main(String a[]) throws Exception {

    Socket sock;


//  ServerSocket serverSocket = new ServerSocket(5556);

    sock = new Socket("127.0.0.1", 4444);
    System.out.println("Connecting...");

    Integer choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Please type 1 for download and 2 for upload."));
    DataOutputStream outs = new DataOutputStream(sock.getOutputStream());



    outs.write(choice);
    //outs.flush();
    //outs.close();

    if(choice == 1){    

    // *****1*****
    //Download
    // Get I/O streams from the socket
    DataInputStream is = new DataInputStream(sock.getInputStream());
    // receive file
    new Client_Test().downloadFile(is);

    }else if(choice == 2){

    //*****2****
    //Upload
    //Send I/O streams to the socket.
    DataOutputStream out = new DataOutputStream(sock.getOutputStream());
    new Client_Test().uploadFile(out);

    }


    sock.close();
}

public void downloadFile(DataInputStream in) throws Exception{

     int filesize=6022386;
      int bytesRead;
      int current = 0;
      byte [] mybytearray  = new byte [filesize];

        FileOutputStream fos = new FileOutputStream("C:\\DowloadTest.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = in.read(mybytearray,0,mybytearray.length);
        current = bytesRead;


        do {
           bytesRead =
              in.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);

        bos.write(mybytearray, 0 , current);
        bos.flush();
        bos.close();


}



public void uploadFile (DataOutputStream out)throws Exception{

    File file = new File("C:\\TestFile.txt");
    FileInputStream fis = new FileInputStream(file);
    byte [] mybytearray  = new byte [(int)file.length()+1];

    BufferedInputStream bis = new BufferedInputStream(fis);

      bis.read(mybytearray,0,mybytearray.length);
      System.out.println("Sending...");
      out.write(mybytearray,0,mybytearray.length);
      out.flush();


      bis.close();

}

}
EN

回答 1

Stack Overflow用户

发布于 2013-02-07 22:24:46

您面临的问题是由以下几行引起的:

代码语言:javascript
复制
if (choice.read() == 1){

代码语言:javascript
复制
  }else if (choice.read() == 2){

流会阻塞,直到数据可用,因此服务器会在第一个if行等待。

当客户端发送1或2时,读调用将继续,并将读取的值与1进行比较。如果发送的值为2,则比较结果为假,服务器将继续到第二行,并阻塞等待更多输入。

您应该将两个read调用替换为一个调用:

代码语言:javascript
复制
int choiceValue = choice.read();
if (choiceValue==1)
{
}
else if (choiceValue==2
{
}     

这应该可以解决您当前的问题。

我认为在您的Server_Test.receiveFile()方法中可能存在另一个问题,因为您似乎正在尝试读取文件两次,一次在此行

代码语言:javascript
复制
bytesRead = in.read(mybytearray,0,mybytearray.length);

然后在循环中再次执行:

代码语言:javascript
复制
do {
           bytesRead =
              in.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14752218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档