我正在研究Java Socket
,我尝试使用端口80开发Socket
以从浏览器下载文件。
所以,我运行我的主类(下面的源代码),它会在我想要的任何端口打开Socket
。
然后,外面的人会访问http://MY_IP:MY_PORT/download/FILE_NAME
我得到了这一切,但客户端的文件大小为0字节(对于小文件),对于较大的档案(原始600mb,下载540mb + - )
我经常检查我的代码很多次,我找不到任何错误,我也从java libs改为Apache-commons,认为这会有所帮助,但没有成功。
所以也许我认为我在Response标题上出了问题。
你能帮帮我吗? 提前谢谢。班级HTTPDownload
:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class HTTPDownloader {
Socket incoming = null;
ServerSocket server = null;
public HTTPDownloader(){
int port = 11000;
try{
server = new ServerSocket(port);
System.out.println("Creating SocketServer on Port " + port);
}catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Preparing to accept connections...");
while(true){
try{
incoming = server.accept();
System.out.println("connection!");
HTTPDownloaderThread thread1 = new HTTPDownloaderThread(incoming);
thread1.start();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException{
new HTTPDownloader();
}
}
班级HTTPDownloadThread
:
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Paths;
class HTTPDownloaderThread extends Thread {
private static final int BUFFER_SIZE = 4096;
private Socket socket;
private byte[] buf = new byte[BUFFER_SIZE];
private OutputStream out;
private InputStream is;
HTTPDownloaderThread(final Socket socket){
this.socket = socket;
}
public void run(){
int numberRead = 0;
try{
out = socket.getOutputStream();
is = socket.getInputStream();
numberRead = is.read(buf, 0, BUFFER_SIZE);
System.out.println("read " + numberRead);
if(numberRead<0)
return;
byte[] readBuf = new byte[numberRead];
System.arraycopy(buf, 0, readBuf, 0, numberRead);
String header = new String(readBuf);
System.out.println(header);
String fileName = header.split("\r\n")[0].split(" ")[1].substring(1);
System.out.println(socket.getInetAddress().getHostAddress()+" asked for file: "+fileName);
File f = new File("C:\\TestFolder\\"+fileName);
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Accept-Ranges: bytes\r\n".getBytes());
out.write(("Content-Length: "+f.length()+"\r\n").getBytes());
out.write("Content-Type: application/octet-stream\r\n".getBytes());
out.write(("Content-Disposition: attachment; filename=\""+fileName+"\"\r\n").getBytes());
out.write("\r\n".getBytes()); // Added as Joy Rê proposed, make it work!
Files.copy(Paths.get("C:\\TestFolder\\"+fileName) , out);
System.out.println("File upload completed!");
// out.flush();
out.close();
socket.close();
}catch(SocketException e) {
System.out.println(e.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
首先,在标题和数据之间添加另一个“\ r \ n”。检查您的HTTP响应; Content-Length标头是否报告下载文件的正确文件大小?这些文件是否可以像在服务器上一样在客户端上显示? Web代理总是有助于调试HTTP(或其他客户端 - 服务器)应用程序:)
另外,我假设您在浏览器上指定端口11000,因为这是您在服务器上监听的内容
答案 1 :(得分:0)
该网站不允许我发表评论,但我认为我应该告诉我的调查结果......使用
Files.copy("path",outStreamObj);
outStreamObj.close();
socketObj.close();
会导致下载不完整或损坏,但是如果仍然想要使用那么outStreamObj和socketObj一定不能关闭,文件传输速度很快与上面的代码(至少从我的观察)。如果您尝试关闭它将报告Broken Pipe或Connection reset,或者不会完成下载(冻结)。
相反,使用以下代码将允许您将outStreamObj关闭为socketObj,但文件下载从套接字缓慢可能是因为while循环。
Socket socket=serverSocket.accept();
FileInputStream fs=new FileInputStream(path);
OutputStream out = socket.getOutputStream();
//This is the change from the Files.copy()
int reads=0;
while((reads=fs.read())!=-1)
{
out.write(reads);
}
out.close();
socket.close();