简体   繁体   English

Java请求文件,发送文件(客户端服务器)

[英]Java request file, send file (Client-server)

I'm making a Client-Server. 我正在做一个客户端服务器。 I've gotten as far as that the server can send a hardcoded file, but not a client specified. 我已经了解到服务器可以发送硬编码文件,但不能指定客户端。 I will have to send only text files. 我将只发送文本文件。 As far as I have understood: the clients firstly sends the file name and then, the server sends it, nothing complicated, but I'm getting all kinds of errors, this code is getting a connection reset/socket closed error. 据我了解:客户端首先发送文件名,然后服务器发送文件名,没有什么复杂的,但是我遇到了各种各样的错误,此代码是连接重置/套接字关闭错误。 The main problem is, that hadn't got much time to research networking. 主要问题是,没有太多时间来研究网络。

Ill appreciate any help I can get. 我会很感激我能得到的任何帮助。

EDIT. 编辑。 I found a work around, closing a stream causes the socket to close, why is that? 我找到了解决方法,关闭流会导致套接字关闭,为什么? It shouldn't happen, should it? 它不应该发生,对吗?

Server Side: 服务器端:

    InputStream sin=newCon.getInputStream();
    DataInputStream sdata=new DataInputStream(sin);
    location=sdata.readUTF();   
    //sdata.close();
    //sin.close();

File toSend=new File(location);
byte[] array=new byte[(int)toSend.length()];
FileInputStream fromFile=new FileInputStream(toSend);
BufferedInputStream toBuffer=new BufferedInputStream(fromFile);
toBuffer.read(array,0,array.length);

OutputStream out=newCon.getOutputStream(); //Socket-closed...
out.write(array,0,array.length);
out.flush();
toBuffer.close();
newCon.close();

ClientSide: 客户端:

int bytesRead;
server=new Socket(host,port);

OutputStream sout=server.getOutputStream();
DataOutputStream sdata=new DataOutputStream(sout);
sdata.writeUTF(interestFile);
//sdata.close();
//sout.close();

InputStream in=server.getInputStream();     //socket closed..
OutputStream out=new FileOutputStream("data.txt");
byte[] buffer=new byte[1024];
while((bytesRead=in.read(buffer))!=-1)
{
    out.write(buffer,0,bytesRead);
}
out.close();
server.close();

Try reading the file in chunks from Server while writing to client output stream rather than creating a temp byte array and reading entire file into memory. 在写入客户端输出流时,尝试从服务器中分块读取文件,而不是创建临时字节数组并将整个文件读入内存。 What if requested file is large? 如果请求的文件很大怎么办? Also close the new Socket on server-side in a finally block so socket is closed even if an exception is thrown. 还要在finally块中在服务器端关闭新的Socket,以便即使抛出异常也关闭套接字。

Server Side: 服务器端:

    Socket newCon = ss.accept();
    FileInputStream is = null;
    OutputStream out = null;
    try {
        InputStream sin = newCon.getInputStream();
        DataInputStream sdata = new DataInputStream(sin);
        String location = sdata.readUTF();
        System.out.println("location=" + location);
        File toSend = new File(location);
        // TODO: validate file is safe to access here
        if (!toSend.exists()) {
            System.out.println("File does not exist");
            return;
        }
        is = new FileInputStream(toSend);
        out = newCon.getOutputStream();
        int bytesRead;
        byte[] buffer = new byte[4096];
        while ((bytesRead = is.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        if (out != null)
            try {
               out.close();
            } catch(IOException e) {
            }
        if (is != null)
            try {
               is.close();
            } catch(IOException e) {
            }
        newCon.close();
    }

If you use Apache Common IOUtils library then you can reduce much of the code to read/write files to streams. 如果使用Apache Common IOUtils库,则可以减少许多代码来将文件读/写到流。 Here 5-lines down to one line. 在这里5线下降到1线。

org.apache.commons.io.IOUtils.copy(is, out);

Note that having a server that serves files by absolute path to remote clients is potentially dangerous and the target file should be restricted to a given directory and/or set of file types. 请注意,拥有通过绝对路径到远程客户端的文件来提供文件的服务器是潜在的危险,目标文件应限于给定的目录和/或文件类型集。 Don't want to serve out system-level files to unauthenticated clients. 不想将系统级文件提供给未经身份验证的客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM