简体   繁体   English

使用SOCKET的Java Server通过同一台计算机上的浏览器将文件上传下载到其中

[英]Java Server using SOCKET to download a file upload to it via a browser on the same machine1

I have a java server using sockets. 我有一个使用套接字的Java服务器。 I have an html file which contains a form that can be used to upload a file and send it. 我有一个html文件,其中包含可用于上传和发送文件的表格。 How can the server download that file without blocking. 服务器如何在不阻止的情况下下载该文件。 The browser never closes the connection and it just hangs in there I have beeping using readLine() from the input stream but it blocks. 浏览器从不关闭连接,它只是挂在这里,我在输入流中使用readLine()发出哔哔声,但它阻塞了。 is there any way around this? 有没有办法解决?

I appreciate any help here. 感谢您的帮助。 Thanks 谢谢

please clarify your requirement, it seems total chaos in your steps. 请阐明您的要求,似乎您的步骤完全混乱。 1. did your program run as client side or server side? 1.您的程序是以客户端还是服务器端运行的? 2. could you show your code? 2.您可以显示您的代码吗? and show what blocked you? 并显示什么阻止了您? 3. It is much better if you can draw a working-flow of your program . 3.如果可以绘制程序的工作流程,那就更好了。


Thanks for your supplement, do you want to upload a files through HTTP(writing in socket way). 感谢您的补充,您是否要通过HTTP(以套接字方式写入)上传文件。 For this requirement, you could check the link link for how HTTP works [RFC 1867] ( https://tools.ietf.org/html/rfc1867 ) 对于此要求,您可以检查链接链接以了解HTTP的工作方式[RFC 1867]( https://tools.ietf.org/html/rfc1867

Below is the package of how socket way do: Suppose the server supplies the following HTML: 下面是套接字方式的软件包:假设服务器提供以下HTML:

 <FORM ACTION="http://server.dom/cgi/handle"
       ENCTYPE="multipart/form-data"
       METHOD=POST>
 What is your name? <INPUT TYPE=TEXT NAME=submitter>
 What files are you sending? <INPUT TYPE=FILE NAME=pics>
 </FORM>

and the user types "Joe Blow" in the name field, and selects a text file "file1.txt" for the answer to 'What files are you sending?' 然后用户在名称字段中输入“ Joe Blow”,并选择文本文件“ file1.txt”作为“您要发送什么文件?”的答案。

The client might send back the following data: 客户端可能会发回以下数据:

    Content-type: multipart/form-data, boundary=AaB03x

    --AaB03x
    content-disposition: form-data; name="field1"

    Joe Blow
    --AaB03x
    content-disposition: form-data; name="pics"; filename="file1.txt"
    Content-Type: text/plain

     ... contents of file1.txt ...
    --AaB03x--

If the user also indicated an image file "file2.gif" for the answer to 'What files are you sending?', the client might client might send back the following data: 如果用户还为“您要发送什么文件?”的答案指定了一个图像文件“ file2.gif”,则客户端可能会向客户端发送以下数据:

    Content-type: multipart/form-data, boundary=AaB03x

    --AaB03x
    content-disposition: form-data; name="field1"

    Joe Blow
    --AaB03x
    content-disposition: form-data; name="pics"
    Content-type: multipart/mixed, boundary=BbC04y

    --BbC04y
    Content-disposition: attachment; filename="file1.txt"

Nebel & Masinter Experimental [Page 9] Nebel&Masinter实验[第9页]

RFC 1867 Form-based File Upload in HTML November 1995 RFC 1867 HTML中基于表单的文件上传1995年11月

    Content-Type: text/plain

    ... contents of file1.txt ...
    --BbC04y
    Content-disposition: attachment; filename="file2.gif"
    Content-type: image/gif
    Content-Transfer-Encoding: binary

      ...contents of file2.gif...
    --BbC04y--
    --AaB03x--

I have server running on port. 我的服务器在端口上运行。 I want it to gather the data sent by a POST request which carries a file (Not just text but png image). 我希望它收集由POST请求发送的数据,该请求携带一个文件(不只是文本,而是png图像)。 for example here is a server: 例如,这是一台服务器:

public void run() {
    try {
        InputStream is = insocket.getInputStream();
        PrintWriter out = new PrintWriter(insocket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String line;
        line = in.readLine();
        String request_method = line;
        System.out.println("HTTP-HEADER: " + line);
        line = "";
        // looks for post data
        int postDataI = -1;
        while ((line = in.readLine()) != null && (line.length() != 0)) {
            System.out.println("HTTP-HEADER: " + line);
            if (line.indexOf("Content-Length:") > -1) {
                postDataI = new Integer(
                        line.substring(
                                line.indexOf("Content-Length:") + 16,
                                line.length())).intValue();
            }
        }
        String postData = "";
        // read the post data
        if (postDataI > 0) {
            char[] charArray = new char[postDataI];
            in.read(charArray, 0, postDataI);
            postData = new String(charArray);
        }
        out.println("HTTP/1.0 200 OK");
        out.println("Content-Type: text/html; charset=utf-8");
        out.println("Server: MINISERVER");
        // this blank line signals the end of the headers
        out.println("");
        // Send the HTML page
        out.println("<H1>Welcome to the Mini Server</H1>");
        out.println("<H2>Request Method->" + request_method + "</H2>");
        out.println("<H2>Post->" + postData + "</H2>");
        out.println("<form name=\"input\" action=\"form_submited\" method=\"post\">");
        out.println("Username: <input type=\"text\" name=\"user\"><input type=\"submit\" value=\"Submit\"></form>");
        out.close();
        insocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

How can it be changed to safe a file (Uploaded through HTTP/1.1 POST REQUEST) form) to disk using this server. 如何使用该服务器将其更改为将文件(通过HTTP / 1.1 POST REQUEST上载)安全保存到磁盘。 Thank you for your time 感谢您的时间

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

相关问题 Java-使用套接字通过浏览器下载文件 - Java - Download a file through browser using a Socket 如何使用Java将文件从ftp服务器下载到本地计算机 - How to download a file from ftp server to local machine using java 如何使用Java Socket从服务器下载文件? - How to download a file from a server using Java Socket? 使用Java中的套接字将文件从服务器下载到客户端 - Download file from server to client using socket in java 如何从客户端计算机下载zip文件,而不是使用Java在浏览器中打开文件? - how to download zip-file from client machine instead of opening file in browser using java? 通过JAVA使用XQuery eval将文件上传到MarkLogic Server文件系统 - Upload file to the MarkLogic Server filesystem using XQuery eval via JAVA 一台机器可以使用Java中的套接字同时充当客户端和服务器吗? - Can one machine act as client and server at same time using socket in Java? 如何使用客户端机器的 Java 套接字在服务器机器上创建目录? - How to create a directory on a server machine using Java socket of a client machine? 服务器中的文件意外结束,但通过浏览器正确下载了文件 - Unexpected end of file from server but file download correctly via browser 使用Java / spring在浏览器中下载文件 - Download a file in browser using java /spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM