Java FileOutputStream写入0字节
我试图通过Java套接字将文件从一个程序发送到另一个程序。我的代码基于问题是,在托管服务器的Ubuntu机器上,发送的文件大小为0字节。代码在我的Windows笔记本电脑上的本地连接上起作用,所以问题可能与远程连接有关 服务器代码:Java FileOutputStream写入0字节,java,sockets,ubuntu,server,fileoutputstream,Java,Sockets,Ubuntu,Server,Fileoutputstream,我试图通过Java套接字将文件从一个程序发送到另一个程序。我的代码基于问题是,在托管服务器的Ubuntu机器上,发送的文件大小为0字节。代码在我的Windows笔记本电脑上的本地连接上起作用,所以问题可能与远程连接有关 服务器代码: int g = Integer.parseInt(in.readLine()); if(g > -1) { InputStream fIn = client.getInputStream(); for(int i = 0; i < g;
int g = Integer.parseInt(in.readLine());
if(g > -1) {
InputStream fIn = client.getInputStream();
for(int i = 0; i < g; i++) {
String name = in.readLine();
byte[] bytes = new byte[16*1024];
File f = new File("plugins/" + name + ".jar");
if(!f.exists()) f.createNewFile();
FileOutputStream fOut = new FileOutputStream(f);
int count;
while ((count = fIn.read(bytes)) >= 0) {
fOut.write(bytes, 0, count);
}
fOut.flush();
fOut.close();
}
System.out.println("[" + client.getRemoteSocketAddress().toString() + "] added " + g + " new plugins.");
client.close();
}else{
client.close();
}
正如评论中所说,您的代码中存在不同的问题 要识别单个文件的结尾,只需在文件名后添加文件大小,并用分号分隔
out.println(f.getName().split("\\.")[0] + "; size" + f.length());
在服务器端,累积文件大小读/写停止读取文件内容关闭文件并读取下一行(文件名/文件大小)
正如评论中所说,存在不同的问题。 您使用的
fIn
和fOut
不清楚它们是如何定义的。
在客户端,您使用fOut
,而套接字输出流被命名为“out”。
服务器端也有类似的情况。您从中的读取文件数,但之后您从套接字获取输入流(再次?),并将其用作fIn
。取决于您在
中的,这可能有效,也可能无效
通过readLine
从InputStream
读取行以及二进制内容也不是那么简单。在我的示例代码中,我使用了DataInputStream
,因为即使是readLine
方法也不推荐使用
该示例还使用“try with resources”语法来确保在出现异常时关闭所有资源(流、套接字)
该示例没有显示如何处理服务器端由您决定的文件大小
该示例完全可以在一台机器上运行,并展示了如何基于代码通过套接字复制单个文件
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ClientServerSocketExample {
private static class Server {
public void run() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(41000, 10, InetAddress.getLocalHost())) {
try (Socket client = serverSocket.accept()) {
try (DataInputStream in = new DataInputStream(client.getInputStream())) {
int g = Integer.parseInt(in.readLine());
if(g > -1) {
for(int i = 0; i < g; i++) {
String[] filenameAndSize = in.readLine().split(";");
String name = filenameAndSize[0];
byte[] bytes = new byte[16*1024];
File f = new File("/tmp/" + name + ".jar");
if(!f.exists()) f.createNewFile();
try (FileOutputStream fOut = new FileOutputStream(f)) {
int count;
while ((count = in.read(bytes)) >= 0) {
fOut.write(bytes, 0, count);
}
fOut.flush();
}
}
System.out.println("[" + client.getRemoteSocketAddress().toString() + "] added " + g + " new plugins.");
}
}
}
}
}
}
private static class Client {
public void run() throws IOException {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 41000) );
sendFiles(socket);
}
}
private static void sendFiles(Socket sock) throws IOException {
File[] files = new File[]{new File("some.jar")};
OutputStream fOut = sock.getOutputStream();
PrintStream out = new PrintStream(fOut);
out.println(files.length);
for(File f : files) {
out.println(f.getName().split("\\.")[0] + "; size" + f.length());
byte[] bytes = new byte[16 * 1024];
try (FileInputStream fIn = new FileInputStream(f)) {
int count;
while ((count = fIn.read(bytes)) >= 0) {
out.write(bytes, 0, count);
}
out.flush();
}
}
}
}
public static void main(String ... args) throws IOException, InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
try {
new Server().run();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}).start();
System.out.println("Waiting for the Server to come up");
Thread.sleep(500);
new Client().run();
}
}
import java.io.DataInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.io.PrintStream;
导入java.net.InetAddress;
导入java.net.InetSocketAddress;
导入java.net.ServerSocket;
导入java.net.Socket;
公共类ClientServerSocketExample{
专用静态类服务器{
public void run()引发IOException{
try(ServerSocket ServerSocket=newserversocket(41000,10,InetAddress.getLocalHost())){
try(Socket client=serverSocket.accept()){
try(DataInputStream in=newdatainputstream(client.getInputStream())){
int g=Integer.parseInt(in.readLine());
如果(g>-1){
对于(int i=0;i=0){
四次写入(字节,0,计数);
}
fOut.flush();
}
}
System.out.println(“[”+client.getRemoteSocketAddress().toString()+“]添加了“+g+”新插件”);
}
}
}
}
}
}
私有静态类客户端{
public void run()引发IOException{
try(套接字=新套接字()){
connect(新的InetSocketAddress(InetAddress.getLocalHost(),41000));
发送文件(套接字);
}
}
私有静态void sendFiles(套接字sock)引发IOException{
File[]files=新文件[]{new File(“some.jar”)};
OutputStream fOut=sock.getOutputStream();
打印流输出=新打印流(fOut);
out.println(files.length);
用于(文件f:文件){
out.println(f.getName().split(“\\”)[0]+“size”+f.length());
字节[]字节=新字节[16*1024];
try(FileInputStream fIn=newfileinputstream(f)){
整数计数;
而((计数=fIn.read(字节))>=0){
out.write(字节,0,计数);
}
out.flush();
}
}
}
}
公共静态void main(字符串…参数)引发IOException、InterruptedException{
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
新建服务器().run();
}捕获(ioe异常ioe){
系统输出打印项次(ioe);
}
}
}).start();
System.out.println(“等待服务器启动”);
睡眠(500);
新客户端().run();
}
}
当客户端将\r
作为新行分隔符发送,并且文件以\n
开头时,此解决方案中可能存在问题。服务器可能会将\n
计算为行分隔符的一部分,文件将损坏(缺少一个字节) 客户端是什么类型的对象?第一个元素/行是文件数。添加读取文件数的部件?代码中存在不同的问题:流中没有分隔符告诉服务器文件的所有字节都已读取并且新文件开始。您的异常/资源处理。你不能关闭你所有的门
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ClientServerSocketExample {
private static class Server {
public void run() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(41000, 10, InetAddress.getLocalHost())) {
try (Socket client = serverSocket.accept()) {
try (DataInputStream in = new DataInputStream(client.getInputStream())) {
int g = Integer.parseInt(in.readLine());
if(g > -1) {
for(int i = 0; i < g; i++) {
String[] filenameAndSize = in.readLine().split(";");
String name = filenameAndSize[0];
byte[] bytes = new byte[16*1024];
File f = new File("/tmp/" + name + ".jar");
if(!f.exists()) f.createNewFile();
try (FileOutputStream fOut = new FileOutputStream(f)) {
int count;
while ((count = in.read(bytes)) >= 0) {
fOut.write(bytes, 0, count);
}
fOut.flush();
}
}
System.out.println("[" + client.getRemoteSocketAddress().toString() + "] added " + g + " new plugins.");
}
}
}
}
}
}
private static class Client {
public void run() throws IOException {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 41000) );
sendFiles(socket);
}
}
private static void sendFiles(Socket sock) throws IOException {
File[] files = new File[]{new File("some.jar")};
OutputStream fOut = sock.getOutputStream();
PrintStream out = new PrintStream(fOut);
out.println(files.length);
for(File f : files) {
out.println(f.getName().split("\\.")[0] + "; size" + f.length());
byte[] bytes = new byte[16 * 1024];
try (FileInputStream fIn = new FileInputStream(f)) {
int count;
while ((count = fIn.read(bytes)) >= 0) {
out.write(bytes, 0, count);
}
out.flush();
}
}
}
}
public static void main(String ... args) throws IOException, InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
try {
new Server().run();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}).start();
System.out.println("Waiting for the Server to come up");
Thread.sleep(500);
new Client().run();
}
}