使用HTTP从Java客户端以Java格式上载文件

时间:2014-10-13 06:12:53

标签: java file-upload

我的客户端代码是在java中。它根据请求将文件上传到不同的服务器。这些要求发生在诸如"印度到美国","美国到英国"等国家之间。此外,有时文件大小为2 GB。我当前的代码是无效的,因为它上传文件很慢,并且要求你提供1 GB的堆空间,甚至只上传60 MB的文件。

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    //System.out.println("Uploading the file");
    String responseFromServer = "";       
    String urlString = "http://stcss.us.com:8888/codesign/UploadServlet";       


    try {
        //------------------ CLIENT REQUEST
        FileInputStream fileInputStream = 
            new FileInputStream(new File(exsistingFileName));
        int file_size = fileInputStream.available();

        if (file_size > 1000 * 1024 * 1024) {
            System.out.println("File Size  Error\n Max Size allowed is 500MB");
            System.exit(1);
        }

        // open a URL connection to the Jsp
        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);
    //conn.setRequestProperty("Accept", "application/octet-stream");
    conn.setRequestProperty("password", password);
        conn.setRequestProperty("signingParameters", signingParameters);
        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"upload\";" + 
                       " filename=\"" + exsistingFileName + "\"" + 
                       lineEnd);
        dos.writeBytes(lineEnd);
        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // read file and write it into form...
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0) {
            //   System.out.println ("file size is"+file_size+"avaiable is"+bytesAvailable);
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // close streams
        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (MalformedURLException ex) {
        System.out.println("From CLIENT CLIENT REQUEST Malformed:" + ex);
        System.exit(0);
    } catch (IOException ioe) {
        System.out.println("From CLIENT CLIENT REQUEST:" + ioe);
        System.exit(0);
    }

我可以通过哪些方法更改此代码以加快上传速度,还可以为较小的文件摆脱堆空间?是否需要在服务器端更改某些内容?我在服务器端使用apache-commons-fileupload。

0 个答案:

没有答案