POST多部分/表单数据内容乱序

时间:2018-03-07 13:19:05

标签: sockets http post

我正在尝试使用java套接字构建一个Web服务器,一切都很好,除非浏览器发送附带文件的POST请求,当收到请求时文件的内容乱序,发送的文件是txt文件收到行号时,行号无序。有什么方法我可以避免这个我想要的有序数据(见图99522,然后是99712)谢谢

public class Server{

public static void main(String[] args) throws IOException {
    ServerSocket server = new ServerSocket(8080);
    while(true) new Thread(new Client(server.accept())).start();
}

}


class Client implements Runnable {

Socket client;
OutputStream os = new FileOutputStream(new File("File12"));
InputStream in;
OutputStream out;
static ArrayList<Socket> clients = new ArrayList<Socket>();
String index;
String response;

Client(Socket client) throws IOException {
    String listOfFiles = "<ol>";
    this.client = client;
    in = client.getInputStream();
    out = client.getOutputStream();
    clients.add(client);
    for (File file : new File(".").listFiles()) if (file.isFile()) listOfFiles += "<li><a href=" + URLEncoder.encode(file.getName(), "UTF-8") + ">" + file.getName() + "</a></li>"; 
    listOfFiles += "</ol>";
    index = "<!DOCTYPE html><html><body><h1>" + new Date() + "</h1><hr>"+listOfFiles+"<form id='upload' enctype='multipart/form-data' method='post' action='/upload'><input id='fileupload' multiple name='myfile' type='file' /><input type='submit' value='submit' id='submit' /></form></body></html>";  
    response = "HTTP/1.1 200 OK\r\n" +
                "Content-Type: text/html\r\n" +
                "Content-Length:"+index.length()+"\r\n" +
                "\r\n" +
                index;

}

public void run() {

    try {
        String msg = "";
        byte buffer[] = new byte[32*1024];
        int read = in.read(buffer);
        while(read != -1){
            msg = new String(buffer);
            System.out.println(msg);
            if(msg.startsWith("POST")){
                System.err.println("RAN IN POST");
                out.write("HTTP/1.1 200 OK\r\n".getBytes());
                out.write("Content-Type: text/plain\r\n".getBytes());
                out.write(("Content-Length:"+ 4 +"\r\n").getBytes());
                out.write("\r\n".getBytes());
                out.write("done".getBytes());

            }
            if(msg.startsWith("GET")){
                String path = msg.substring(msg.indexOf("/"), msg.indexOf("HTTP")).trim();
                if(path.equals("/")) out.write(response.getBytes());
                else {
                    String fileName = path.substring(1);
                    fileName = URLDecoder.decode(fileName,"UTF-8");
                    System.out.println(fileName);
                    File file = new File(fileName);
                    if(file.exists()){
                        System.out.println(file.getName() + " " + file.length());
                        out.write("HTTP/1.1 200 OK\r\n".getBytes());
                        out.write("Accept-Ranges: bytes\r\n".getBytes());
                        out.write(("Transfer-Encoding: chunked\r\n").getBytes());
                        out.write("Content-Type: application/octet-stream\r\n".getBytes());
                        out.write(("Content-Disposition: attachment; filename=\""+file.getName()+"\"\r\n").getBytes());
                        out.write("\r\n".getBytes()); 
                        try{
                            Files.copy(Paths.get(file.getPath()) , out);
                        }catch(Exception e){
                            break;
                        }
                    }else System.out.println("file not existes");
                }
            }
            out.flush();
            os.close();
            read = in.read(buffer);
        }
        System.err.println("closing scoket");
        out.close();
        in.close();
        client.close();
        clients.remove(client);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

enter image description here

1 个答案:

答案 0 :(得分:0)

所有权利发现了我没有清除缓冲区的错误因此它再次出现