简体   繁体   English

如何处理从Java客户端上传服务器端HTTP文件

[英]How to handle Server side HTTP file upload from Java client

I want to upload a file from a client to a server. 我想将文件从客户端上传到服务器。

Client: Java using HTTP post Server: Java Servlet 客户端:使用HTTP发布的Java服务器:Java Servlet

I have added the client side coding here. 我在这里添加了客户端编码。 But, i don't have any idea with the server side processing. 但是,我对服务器端处理一无所知。 Please do help me with a code snippet. 请帮助我提供代码段。

 private String Tag = "UPLOADER";
    private String urlString = "http://192.168.42.140:8080/sampweb";
    HttpURLConnection conn;
    String exsistingFileName = "/sdcard/log.txt";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    try {
        // ------------------ CLIENT REQUEST

        Log.e(Tag, "Inside second Method");

        FileInputStream fileInputStream = new FileInputStream(new File(
                exsistingFileName));

        // open a URL connection to the Servlet

        URL url = new URL(urlString);

        // Open a HTTP connection to the URL

        conn = (HttpURLConnection) url.openConnection();

        // Allow Inputs
        conn.setDoInput(true);

        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos
                .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                        + exsistingFileName + "" + lineEnd);
        dos.writeBytes(lineEnd);

        Log.e(Tag, "Headers are written");

        // create a buffer of maximum size

        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1000;
        // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bytesAvailable];

        // read file and write it into form...

        int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bytesAvailable);
            bytesAvailable = fileInputStream.available();
            bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
        }

        // send multipart form data necessary after file data...

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        Log.e(Tag, "File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();

    } catch (MalformedURLException ex) {
        Log.e(Tag, "error: " + ex.getMessage(), ex);
    }

    catch (IOException ioe) {
        Log.e(Tag, "error: " + ioe.getMessage(), ioe);
    }

    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                .getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            Log.e("Dialoge Box", "Message: " + line);
        }
        rd.close();

    } catch (IOException ioex) {
        Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
    }
}

I'm new to the server programming. 我是服务器编程的新手。 If i have done any mistakes do clarify me. 如果我做任何错误,请澄清一下。 Thanks! 谢谢!

The best library to achieve this is still the Apache Commons File Upload . 实现此目标的最佳库仍然是Apache Commons File Upload It is well documented and easy to use. 它有据可查且易于使用。 If you ran into any issues, please check FAQ first. 如果您遇到任何问题,请先查看常见问题解答

暂无
暂无

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

相关问题 将文件从 Java 客户端上传到 HTTP 服务器 - Upload files from Java client to a HTTP server 使用ajax和spring portlet将文件从客户端上传到服务器端 - Upload a file from client side to server side with ajax and spring portlet 在java web应用程序中如何从客户端打印服务器文件 - In java web application how to print server file from client side Java客户端如何将文件上传到远程HDFS服务器或从远程HDFS服务器下载文件? - How a Java client upload/download a file to/from remote HDFS server? 如何处理服务器端从移动应用程序上传的文件? - How do I handle a file upload from a mobile application on the server side? 如何在服务器端使用 java 从客户端的 HTTP POST 请求中接收和解析 JSON 对象 - How to receive and parse a JSON object from a HTTP POST request from client using java on the server side 如何在服务器端而非客户端的JAVA中逐行从config.properties文件读取数据 - How to read data from config.properties file line by line in JAVA on the server side not on the client side 如何将图像直接从客户端上传到服务器 - how to upload image directly from client side to server 使用Dojo文件上传客户端将其他数据发送到Java服务器端 - Send additional data to Java server-side using Dojo file upload client-side 如何正确处理 HTTP 文件服务器上的客户端“连接:关闭”请求? - How to properly handle client “Connection: close” request on HTTP file server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM