简体   繁体   中英

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. 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

   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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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