0

我有一个为我的 Java 类工作的项目,我正在使用 Socket 是的,我在网上查看教程寻求帮​​助,目的是在 pdf 类型的服务器上读取文件,然后允许客户端从服务器请求此文件。

我的问题它请求文件,但是当我点击要启动的文件时请求文件并将其存储在客户端计算机上之后,adobe 说文件“Adobe 无法打开文件,因为它不受支持的文件类型或因为文件已损害

这是我的代码如果有人可以帮助我,我将不胜感激:

服务器代码:

import java.io.*;
import java.net.*;

public class SimpleFileServer {

 public final static String FILE_TO_SEND = "‪c:/Users/Acer/Downloads/COAFlags.pdf"; // you may change this

public static void main(String args[]) {

    while (true) {
        ServerSocket welcomeSocket = null;
        Socket connectionSocket = null;
        BufferedOutputStream outToClient = null;

        try {
            welcomeSocket = new ServerSocket(3248);
            connectionSocket = welcomeSocket.accept();
            outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
        } catch (IOException ex) {
            // Do exception handling
        }

        if (outToClient != null) {
            File myFile = new File( FILE_TO_SEND);
            byte[] mybytearray = new byte[(int)myFile.length()];

            FileInputStream fis = null;

            try {
                fis = new FileInputStream(myFile);
            } catch (FileNotFoundException ex) {
                // Do exception handling
            }
            BufferedInputStream bis = new BufferedInputStream(fis);

            try {
                bis.read(mybytearray, 0, mybytearray.length);
                outToClient.write(mybytearray, 0, mybytearray.length);
                outToClient.flush();
                outToClient.close();
                connectionSocket.close();

                // File sent, exit the main method
                return;
            } catch (IOException ex) {
                // Do exception handling
            }
        }
    }
}}

客户

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.net.*;

public class SimpleFileClient {

private final static String serverIP = "localhost";
public final static int FILE_SIZE = 55000;
private final static int serverPort = 3248;
private final static String fileOutput = "c:/Users/Acer/Downloads/sourcedownloaded.pdf";

public static void main(String args[]) {
    byte[] aByte = new byte[FILE_SIZE];
    int bytesRead;

    Socket clientSocket = null;
    InputStream is = null;

    try {
        clientSocket = new Socket( serverIP , serverPort );
        is = clientSocket.getInputStream();
    } catch (IOException ex) {
        // Do exception handling
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    if (is != null) {

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream( fileOutput );
            bos = new BufferedOutputStream(fos);
            bytesRead = is.read(aByte, 0, aByte.length);

            do {
                    baos.write(aByte);
                    bytesRead = is.read(aByte);
            } while (bytesRead != -1);

            bos.write(baos.toByteArray());
            bos.flush();
            bos.close();
            clientSocket.close();
        } catch (IOException ex) {
            // Do exception handling
        }
    }
}}

我想要传输的文件是 54KB 好的,所以它没有超出范围。

4

2 回答 2

1

这不是你的做法。您必须在 while 循环内传输文件,因此您使用的缓冲区不会使用太多内存。服务器端:

ServerSocket socket = new ServerSocket();
socket.bind(new InetSocketAddress("127.0.0.1",9200));
while(true) //that loop provides server non-stop sending, it will response to client requests till you terminate the application.
{
    Socket received = socket.accept();
    FileInputStream input = new FileInputStream("c:/Users/Acer/Downloads/COAFlags.pdf");
    OutputStream output = received.getOutputStream();
    int length;
    byte[] buffer = new byte[4096];

    while((length = input.read(buffer)) != -1)
    {
        output.write(buffer, 0, length);
    }

    output.close(); //no need to flush because close() already does it
    input.close();
}

客户端:

Socket socket = new Socket("127.0.0.1", 9200);
InputStream input = socket.getInputStream();
FileOutputStream output = new FileOutputStream("c:/Users/Acer/Desktop/abc.pdf");
int length;
byte[] buffer = new byte[4096];

while((length = input.read(buffer)) != -1)
{
    output.write(buffer, 0, length);
}

output.close();
input.close();

注意:缓冲区大小是可选的。一般使用4096。

于 2015-02-02T22:32:31.743 回答
0

新服务器代码:
import java.io。; 导入java.net。;

公共类 SimpleFileServer {

 public final static String FILE_TO_SEND = "‪c:/Users/Acer/Downloads/COAFlags.pdf"; // you may change this

public static void main(String args[]) {

    while (true) {
        ServerSocket welcomeSocket = null;
        Socket connectionSocket = null;
        OutputStream output = null;

        try {
            welcomeSocket = new ServerSocket(3248);
            connectionSocket = welcomeSocket.accept();
            output = connectionSocket.getOutputStream();
        } catch (IOException ex) {
            // Do exception handling
        }

        if (output != null) {



            try {
                int length;
                byte[] buffer = new byte[4096];
                FileInputStream input = new FileInputStream(FILE_TO_SEND);
                try {
                    while((length = input.read(buffer)) != -1)
                    {
                        output.write(buffer, 0, length);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    output.close();
                    input.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //no need to flush because close() already does it

            } catch (FileNotFoundException ex) {
                // Do exception handling
            }



        }
    }
}}

更新的客户端

导入 java.io。; 导入java.net。;

公共类 SimpleFileClient {

private final static String serverIP = "localhost";
public final static int FILE_SIZE = 55000;
private final static int serverPort = 3248;
private final static String fileOutput = "c:/Users/Acer/Downloads/sourcedownloaded.pdf";

public static void main(String args[]) {
    byte[] buffer = new byte[55000];
    int length;

    Socket clientSocket = null;
    InputStream input = null;

    try {
        clientSocket = new Socket( serverIP , serverPort );
        input = clientSocket.getInputStream();
    } catch (IOException ex) {
        // Do exception handling
    }

   // ByteArrayOutputStream baos = new ByteArrayOutputStream();

    if (input != null) {


        FileOutputStream output = null;
        //BufferedOutputStream bos = null;
        try {
            output = new FileOutputStream( fileOutput );
            while((length = input.read(buffer)) != -1)
            {
                output.write(buffer, 0, length);
            }

            output.close();
            input.close();
        } catch (IOException ex) {
            // Do exception handling
        }
    }
}}
于 2015-02-03T14:56:38.010 回答