我有一个为我的 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 好的,所以它没有超出范围。