Java-每行缺少第一个字母
我正在创建文件传输程序,将文件传输到客户端。但是当我传输文件时,它丢失了每行的第一个字母。我的代码怎么了 我对java相当陌生,所以我不知道下一步该做什么。我尝试更改字节大小,但没有帮助。我该怎么办 Server.javaJava-每行缺少第一个字母,java,Java,我正在创建文件传输程序,将文件传输到客户端。但是当我传输文件时,它丢失了每行的第一个字母。我的代码怎么了 我对java相当陌生,所以我不知道下一步该做什么。我尝试更改字节大小,但没有帮助。我该怎么办 Server.java import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; class Server { public static void m
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class Server {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
ServerSocket serverSocket = null;
Scanner scan = new Scanner(System.in);
String fileSend;
System.out.print("Type the path to the file to send >> ");
fileSend = scan.nextLine();
try {
serverSocket = new ServerSocket(5467);
} catch (Exception e) {
System.out.println("Could not bind to port 5467, Maybe address is already is use or you need to run as administrator");
return;
}
System.out.println("Listening on port 5467");
System.out.println("Waiting for the connection...");
while (true) {
File FileSend = null;
Socket socket = serverSocket.accept();
OutputStream out = socket.getOutputStream();
System.out.println("Accepted connection : " + socket);
InputStream in = socket.getInputStream();
DataInputStream dataIn = new DataInputStream(in);
String login = dataIn.readUTF();
String password = dataIn.readUTF();
String result = "You credential is ";
if (login.equals("1c18b5cdef8f9b4c5d6b2ad087265e597d1d4639337b73a04a335103c00ec64b") && password.equals("1c18b5cdef8f9b4c5d6b2ad087265e597d1d4639337b73a04a335103c00ec64b13d0b73358bfa8978dfaaf180565bcfecd3dc0631cda525920865145fb3fa131")) {
result += "correct";
} else {
result += "incorrect";
}
DataOutputStream dataOut = new DataOutputStream(out);
try {
dataOut.writeUTF(result);
} catch (FileNotFoundException e) {
System.out.println("No such file or directory");
}
finally
{
FileSend = new File(fileSend);
byte[] FileByteArray = new byte[(int) FileSend.length()];
try {
fis = new FileInputStream(FileSend);
}
catch (FileNotFoundException e) {
System.out.println("No such file or directory");
return;
}
bis = new BufferedInputStream(fis);
File myFile = new File (fileSend);
byte [] mybytearray = new byte [(int)myFile.length()];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os = socket.getOutputStream();
System.out.println("Sending " + fileSend + "(" + mybytearray.length + " bytes)");
os.write(mybytearray,0,mybytearray.length);
os.flush();
System.out.println("Done.");
}
}
}
}
Client.java
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Client {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
Socket socket = null;
String PlainLogin;
String FileOut;
String PlainPassword;
Scanner scan = new Scanner(System.in);
System.out.print("What is the IP address of the server >> ");
String host = scan.nextLine();
try {
socket = new Socket(host, 5467);
} catch (ConnectException | NullPointerException e) {
System.out.println("Connection Refused, Have you run the server first?");
return;
}
OutputStream out = socket.getOutputStream();
DataOutputStream dataOut = new DataOutputStream(out);
InputStream is = socket.getInputStream();
System.out.println("Connection Established");
System.out.println("Credential Required, Please login");
System.out.print("Type your username >> ");
PlainLogin = scan.next();
System.out.print("Type your password >> ");
PlainPassword = scan.next();
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashInBytes = md.digest(PlainLogin.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : hashInBytes) {
sb.append(String.format("%02x", b));
}
String HashedLogin = sb.toString();
byte[] hashInBytesP = md.digest(PlainPassword.getBytes(StandardCharsets.UTF_8));
for (byte b : hashInBytesP) {
sb.append(String.format("%02x", b));
}
String HashedPassword = sb.toString();
dataOut.writeUTF(HashedLogin);
dataOut.writeUTF(HashedPassword);
InputStream in = socket.getInputStream();
DataInputStream dataIn = new DataInputStream(in);
String str = dataIn.readUTF();
if (str == "Your credential is incorrect") {
System.out.println(str);
return;
} else {
System.out.print("Type any file name you want >> ");
scan.nextLine();
FileOut = scan.nextLine();
OutputStream output = new FileOutputStream(FileOut);
byte[] mybytearray = new byte[1024];
is.read(mybytearray, 0, mybytearray.length);
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
output.write(buffer, 0, bytesRead);
}
output.close();
socket.close();
dataIn.close();
System.out.println("Done");
return;
}
}
}
我希望它像“Java”一样显示全文,但它只显示“ava”文件是可序列化的,所以我建议您只需尝试直接通过ObjectOutputStream/ObjectInputStream发送它 例如,在服务器端:
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
...
out.writeObject(yourFileObject);
和客户:
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
...
File receivedFile = null;
try {
receivedFile = (File) in.readObject();
}
catch (IOException e) {
...
}
文件是可序列化的,所以我建议您只需尝试通过ObjectOutputStream/ObjectInputStream直接发送它 例如,在服务器端:
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
...
out.writeObject(yourFileObject);
和客户:
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
...
File receivedFile = null;
try {
receivedFile = (File) in.readObject();
}
catch (IOException e) {
...
}
我不完全清楚为什么会发生这种情况,但您不应该使用DataInputStream
which“”,而应该使用InputStreamReader
which“”
此外,您还可以将该读取器包装到一个BufferedReader
中,它允许您逐行读取,最后您将得到如下内容
try (BufferedReader reader=new BufferedReader(new InputStreamReader(inStream, UTF8));
PrintWriter writer = new PrintWriter(file)) {
reader.lines().forEach(writer::println);
}
用于上传,或
try (PrintWriter writer = new PrintWriter(outStream)) {
Files.lines().forEach(writer::println);
}
下载。我不完全清楚为什么会发生这种情况,但您不应该使用DataInputStream
which“”,而应该使用InputStreamReader
which“”
此外,您还可以将该读取器包装到一个BufferedReader
中,它允许您逐行读取,最后您将得到如下内容
try (BufferedReader reader=new BufferedReader(new InputStreamReader(inStream, UTF8));
PrintWriter writer = new PrintWriter(file)) {
reader.lines().forEach(writer::println);
}
用于上传,或
try (PrintWriter writer = new PrintWriter(outStream)) {
Files.lines().forEach(writer::println);
}
下载。您也发送UTF编码的数据吗?我的猜测是,第一个字节被视为BOM并被吃掉。查看传入的原始字节数据是否与来自服务器的传出数据匹配。请在((bytesRead=in.read(buffer))>=0的情况下尝试此in循环{您也发送UTF编码的数据吗?我的猜测是第一个字节被视为BOM并被吃掉。查看传入的原始字节数据是否与服务器传出的数据匹配。在((bytesRead=in.read(buffer))>=0的情况下在循环中尝试此操作{