简体   繁体   English

通过Java中的套接字将文件从客户端传输到服务器

[英]Transfer file from client to server over socket in java

I am trying to upload a file from a client to the server using sockets in JAVA. 我正在尝试使用JAVA中的套接字将文件从客户端上传到服务器。 It is partially working, however, the file that gets created on the server is an empty text file. 它部分起作用,但是,在服务器上创建的文件是一个空文本文件。 Can anyone offer any suggestions as to where I may have an issue. 任何人都可以就我可能遇到的问题提供任何建议。 Thanks: 谢谢:

Server: 服务器:

        private void handleFileUpload(String fileSizeInBytes, String fileName) throws IOException{
        String fullyQualifiedFileName = rootDirectory+System.getProperty("file.separator")+fileName;
        File fileToWrite = new File(fullyQualifiedFileName);
        if(fileToWrite.exists()){
            fileToWrite.delete();
        }

        int bytesRead = 0;
        byte[] aByte = new byte[1];
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        ByteArrayOutputStream baos = null;

        try {
            inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(fullyQualifiedFileName);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bytesRead = inputStream.read(aByte, 0, aByte.length);
            baos = new ByteArrayOutputStream();
            do {
                baos.write(aByte);
                bytesRead = inputStream.read(aByte);
            } while (bytesRead != -1);

            bufferedOutputStream.write(baos.toByteArray());
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

Client: 客户:

            private void uploadFile(Socket socket, File fileToUpload){
    byte[] mybytearray = new byte[(int) fileToUpload.length()];
    try {
        FileInputStream fis = new FileInputStream(fileToUpload);
        BufferedOutputStream toServer =  new BufferedOutputStream(socket.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);
        toServer.write(mybytearray, 0, mybytearray.length);
        toServer.flush();
        toServer.close();
        return;
    } catch (IOException ex) {
        handleServerError("upload file", ex);
        System.exit(0);
    }

Change your handleFileUpload method as following 更改您的handleFileUpload方法,如下所示

   private void handleFileUpload(String fileSizeInBytes, String fileName) throws IOException{
        String fullyQualifiedFileName = rootDirectory+System.getProperty("file.separator")+fileName;
        File fileToWrite = new File(fullyQualifiedFileName);
        if(fileToWrite.exists()){
            fileToWrite.delete();
        }

        int bytesRead = 0;
        byte[] aByte = new byte[1024];
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        ByteArrayOutputStream baos = null;

        try {
            inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(fullyQualifiedFileName);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bytesRead = inputStream.read(aByte, 0, aByte.length);
            while (bytesRead != -1) {
                bufferedOutputStream.write(aByte, 0, bytesRead);
                bytesRead = inputStream.read(aByte, 0, aByte.length);
            } 

            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM