使用java中的套接字将文件从服务器下载到客户端

使用java中的套接字将文件从服务器下载到客户端,java,sockets,Java,Sockets,我得到了一些代码,可以将文件从客户端上传到服务器,工作正常,上传的文件保存在名为服务器的服务器文件夹中,现在想将服务器文件夹中存在的文件下载到客户端文件夹,但不知道如何做。 请帮忙 此服务器代码演示了如何将文件从客户端下载到服务器: public static void downloadFile() throws Exception{ try { fileEvent = (FileEvent) inputStream.readObject(); if

我得到了一些代码,可以将文件从客户端上传到服务器,工作正常,上传的文件保存在名为服务器的服务器文件夹中,现在想将服务器文件夹中存在的文件下载到客户端文件夹,但不知道如何做。 请帮忙

此服务器代码演示了如何将文件从客户端下载到服务器:

  public static void downloadFile() throws Exception{
    try {
        fileEvent = (FileEvent) inputStream.readObject();
        if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
            System.out.println("Error occurred ..So exiting");
            System.exit(0);
        }
        String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
        if (!new File(fileEvent.getDestinationDirectory()).exists()) {
            new File(fileEvent.getDestinationDirectory()).mkdirs();
        }

        int filenamelength = dis.readInt();
        String filename = makeString(filenamelength, dis);

        dstFile = new File(path + filename);
        fileOutputStream = new FileOutputStream(dstFile);
        fileOutputStream.write(fileEvent.getFileData());
        fileOutputStream.flush();
        fileOutputStream.close();
        System.out.println("Output file : " + outputFile + " is successfully saved ");
        Thread.sleep(3000);
        System.exit(0);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 public static void sendFile() throws Exception {
    dos.writeInt(6);
    dos.writeChars("upload");

    fileEvent = new FileEvent();
   String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1, sourceFilePath.length());
    String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
    fileEvent.setDestinationDirectory(destinationPath);
    fileEvent.setFilename(fileName);
    fileEvent.setSourceDirectory(sourceFilePath);
    File file = new File(sourceFilePath);
    if (file.isFile()) {
        try {
            DataInputStream diStream = new DataInputStream(new FileInputStream(file));
            long len = (int) file.length();
            byte[] fileBytes = new byte[(int) len];
            int read = 0;
            int numRead = 0;
            while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
                    fileBytes.length - read)) >= 0) {
                read = read + numRead;
            }
            fileEvent.setFileSize(len);
            fileEvent.setFileData(fileBytes);
            fileEvent.setStatus("Success");
        } catch (Exception e) {
            e.printStackTrace();
            fileEvent.setStatus("Error");
        }
    } else {
        System.out.println("path specified is not pointing to a file");
        fileEvent.setStatus("Error");
    }
    //Now writing the FileEvent object to socket
    try {

        outputStream.writeObject(fileEvent);

        System.out.println("Choose file: ");
        String str = scanner.nextLine();

        String[] parts = str.split(Pattern.quote("\\"));
        String filename = parts[parts.length - 1];

        dos.writeInt(filename.length());
        dos.writeChars(filename);

        Thread.sleep(3000);
        System.exit(0);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}
这是将文件从客户端上载到服务器的客户端代码:

  public static void downloadFile() throws Exception{
    try {
        fileEvent = (FileEvent) inputStream.readObject();
        if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
            System.out.println("Error occurred ..So exiting");
            System.exit(0);
        }
        String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
        if (!new File(fileEvent.getDestinationDirectory()).exists()) {
            new File(fileEvent.getDestinationDirectory()).mkdirs();
        }

        int filenamelength = dis.readInt();
        String filename = makeString(filenamelength, dis);

        dstFile = new File(path + filename);
        fileOutputStream = new FileOutputStream(dstFile);
        fileOutputStream.write(fileEvent.getFileData());
        fileOutputStream.flush();
        fileOutputStream.close();
        System.out.println("Output file : " + outputFile + " is successfully saved ");
        Thread.sleep(3000);
        System.exit(0);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 public static void sendFile() throws Exception {
    dos.writeInt(6);
    dos.writeChars("upload");

    fileEvent = new FileEvent();
   String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1, sourceFilePath.length());
    String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
    fileEvent.setDestinationDirectory(destinationPath);
    fileEvent.setFilename(fileName);
    fileEvent.setSourceDirectory(sourceFilePath);
    File file = new File(sourceFilePath);
    if (file.isFile()) {
        try {
            DataInputStream diStream = new DataInputStream(new FileInputStream(file));
            long len = (int) file.length();
            byte[] fileBytes = new byte[(int) len];
            int read = 0;
            int numRead = 0;
            while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
                    fileBytes.length - read)) >= 0) {
                read = read + numRead;
            }
            fileEvent.setFileSize(len);
            fileEvent.setFileData(fileBytes);
            fileEvent.setStatus("Success");
        } catch (Exception e) {
            e.printStackTrace();
            fileEvent.setStatus("Error");
        }
    } else {
        System.out.println("path specified is not pointing to a file");
        fileEvent.setStatus("Error");
    }
    //Now writing the FileEvent object to socket
    try {

        outputStream.writeObject(fileEvent);

        System.out.println("Choose file: ");
        String str = scanner.nextLine();

        String[] parts = str.split(Pattern.quote("\\"));
        String filename = parts[parts.length - 1];

        dos.writeInt(filename.length());
        dos.writeChars(filename);

        Thread.sleep(3000);
        System.exit(0);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}
public static void sendFile()引发异常{
待办事项(6);
dos.writeChars(“上传”);
fileEvent=newfileevent();
字符串fileName=sourceFilePath.substring(sourceFilePath.lastIndexOf(“/”)+1,sourceFilePath.length());
String path=sourceFilePath.substring(0,sourceFilePath.lastIndexOf(“/”)+1);
fileEvent.setDestinationDirectory(destinationPath);
fileEvent.setFilename(文件名);
fileEvent.setSourceDirectory(sourceFilePath);
文件文件=新文件(sourceFilePath);
if(file.isFile()){
试一试{
DataInputStream Distrieam=新DataInputStream(新文件InputStream(文件));
long len=(int)file.length();
字节[]fileBytes=新字节[(int)len];
int read=0;
int numRead=0;
而(read=0){
读取=读取+numRead;
}
setFileSize(len);
fileEvent.setFileData(fileBytes);
fileEvent.setStatus(“成功”);
}捕获(例外e){
e、 printStackTrace();
fileEvent.setStatus(“错误”);
}
}否则{
System.out.println(“指定的路径未指向文件”);
fileEvent.setStatus(“错误”);
}
//现在将FileEvent对象写入套接字
试一试{
outputStream.writeObject(fileEvent);
System.out.println(“选择文件:”);
String str=scanner.nextLine();
String[]parts=str.split(Pattern.quote(“\\”);
字符串文件名=零件[parts.length-1];
dos.writeInt(filename.length());
dos.writeChars(文件名);
睡眠(3000);
系统出口(0);
}捕获(IOE异常){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
  • 您没有说明从哪个API获取
    FileEvent
    ,但您不应该使用这样的技术来假设文件将适合内存:

    long len = (int) file.length();
    byte[] fileBytes = new byte[(int) len];
    

    这里假设(a)
    len我认为实现应该是相同的,因为它们都使用FTP(文件传输协议)。你有什么错误吗?唯一的区别是,客户端可以选择具有文件路径的文件,而在服务器中,您需要指定客户端要下载的文件(路径)
    FileEvent是什么?