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

时间:2016-09-27 04:00:54

标签: java sockets

我正在尝试使用JAVA中的套接字将文件从客户端上传到服务器。它部分工作,但是,在服务器上创建的文件是一个空文本文件。任何人都可以就我可能遇到的问题提出任何建议。感谢:

服务器:

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

客户端:

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

1 个答案:

答案 0 :(得分:0)

按以下方式更改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();
        }
    }