简体   繁体   English

从服务器到客户端的连接,文件传输

[英]Connection from server to client, file transfer

By using instance of PrintStream and println function, I can send raw string to the client(s). 通过使用PrintStreamprintln函数的实例,我可以将原始字符串发送到客户端。 But, I want to send whole .html file to the client in order to see the web page. 但是,我想将整个.html文件发送给客户端,以便查看网页。 For this reason, What should be my approach ? 因此,我应该采取什么方法? I have tried to read a file and give the whatever is read on the println function. 我试图读取一个文件,并给出在println函数上读取的内容。 But, attempts is failed. 但是,尝试失败。

Maby something like this will help: Maby这样的事情会有所帮助:

// sendfile
File myFile = new File ("source.html");
byte [] mybytearray  = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();

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

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