gpt4 book ai didi

java - 文件传输但无法在其各自的启动器中加载(Adobe Pdf) - Java 服务器客户端

转载 作者:行者123 更新时间:2023-12-01 11:57:52 24 4
gpt4 key购买 nike

我有一个为我的 Java 类工作的项目,我正在使用套接字是的,我查看在线教程寻求帮​​助,目的是读取 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,所以它没有超出范围。

最佳答案

这不是你应该做的事。您必须在 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。

关于java - 文件传输但无法在其各自的启动器中加载(Adobe Pdf) - Java 服务器客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28287727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com