使用Java客户端将文件上传到php app引擎

时间:2014-07-29 14:15:38

标签: java php google-app-engine file-upload httpurlconnection

我无法使用Java客户端将文件上传到PHP appengine。我可以通过浏览器上传文件,如this example所示。此外,当我在开发模式下在本地计算机上部署PHP appengine代码时,java客户端仍可正常工作,但不适用于appengine。在appengine上,$ _FILES数组始终为空。这是java代码 -

String uploadFile(String fileName){
    Debug.inform(TAG, "file=" + fileName);
    if (fileName.isEmpty()) {
        return "file_empty";
    }
    String r = "";
    String upload_url = "";
    try {
        Debug.inform(TAG, "origFile = " + fileName);
        String charset = "UTF-8";

        {   // Get upload url
            String param = "Unique-Id";
            String query = String.format("%s=%s", param, URLEncoder.encode(
                        SystemReader.readUniqueId(uniqueId), charset));
            URL url = new URL(Configure.UPLOAD_URL + "?" + query);              
            final HttpURLConnection conn = (HttpURLConnection) url
                .openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Accept-Charset", charset);

            int serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Debug.inform(TAG, "HTTP Response: " + serverResponseMessage
                    + ": " + serverResponseCode);

            if (serverResponseCode == 200) {
                String[] ret = getResult(conn); 
                upload_url = ret[0];
                r = ret[1];
            }
            conn.disconnect();
        }

        if(!upload_url.equals("")) {    
            // Upload the file
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";

            FileInputStream  fileInputStream = new FileInputStream(fileName);
            URL url = new URL(upload_url);              

            final HttpURLConnection conn = (HttpURLConnection) url
                .openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Accept-Charset", charset);
            String shortFileName = fileName.substring(fileName
                    .lastIndexOf("/") + 1);
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=" + boundary);

            //conn.setFixedLengthStreamingMode(32944);

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

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\""
                    + shortFileName + "\"" + lineEnd +
                    "Content-Type: application/octet-stream" + lineEnd);

            dos.writeBytes(lineEnd);
            synchronized (fileWriteLock) {
                int bytesAvailable = fileInputStream.available();
                int maxBufferSize = 1024;
                int bufferSize = Math.min(bytesAvailable, maxBufferSize);
                byte[] buffer = new byte[bufferSize];

                // read file and write it into form...
                int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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

                }
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            fileInputStream.close();
            dos.flush();
            dos.close();

            // Responses from the server (code and message)
            int serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Debug.inform(TAG, "HTTP Response: " + serverResponseMessage
                    + ": " + serverResponseCode);

            conn.disconnect();
        }

    } catch (MalformedURLException ex) {
        Debug.inform(TAG, "MalformedURLException");
        r = "MalformedURLException";

    } catch (IOException ioe) {
        Debug.inform(TAG, "IOException");
        r = "IOException";
    }
}

首次连接会返回一些结果并上传由CloudStorageTools组成的网址。这部分工作正常。下一个连接实际上进行上传。我匹配浏览器发送的http标头和java代码,它们看起来都和我一模一样。

我已经坚持了很长时间。提前谢谢。

0 个答案:

没有答案