0

这是我将文件上传到服务器的客户端代码

try {
    OutputStream output = socket.getOutputStream();     

        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] buffer = new byte[1024*1024];
        int bytesRead = 0;

        while((bytesRead = fileInputStream.read(buffer))>0)
        {
            output.write(buffer,0,bytesRead);
        }
catch (Exception e) {

            } finally {

                //fileInputStream.close();
            }

服务器代码

   File file=null;

    InputStream input = sk.getInputStream();

    file = new File("C://some/somefile.txt" );
    FileOutputStream out = new FileOutputStream(file);

    byte[] buffer = new byte[1024*1024];

    int bytesReceived = 0;

    while((bytesReceived = input.read(buffer))>0) {
        out.write(buffer,0,bytesReceived);
        System.out.println(bytesReceived);
        break;
    }

我上传的文件没有保存在服务器上。我怎样才能更正我的代码?请帮忙?

4

1 回答 1

0

为了正确保存文件,您需要输入:

out.flush();
out.close();

在你写完之后

于 2013-01-25T11:36:16.850 回答