Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java客户端将文件上载到php appengine_Java_Php_Google App Engine_File Upload_Httpurlconnection - Fatal编程技术网

使用Java客户端将文件上载到php appengine

使用Java客户端将文件上载到php appengine,java,php,google-app-engine,file-upload,httpurlconnection,Java,Php,Google App Engine,File Upload,Httpurlconnection,我无法使用Java客户端将文件上载到PHP appengine。我可以通过浏览器上传文件,如所示。另外,当我以开发模式在本地机器上部署PHP appengine代码时,java客户端可以工作,但在appengine上不工作。在appengine上,$\u文件数组始终为空。以下是java代码- String uploadFile(String fileName){ Debug.inform(TAG, "file=" + fileName); if (fileName.isEmpty

我无法使用Java客户端将文件上载到PHP appengine。我可以通过浏览器上传文件,如所示。另外,当我以开发模式在本地机器上部署PHP appengine代码时,java客户端可以工作,但在appengine上不工作。在appengine上,$\u文件数组始终为空。以下是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";
    }
}
第一个连接返回一些结果,并上载从中形成的URL。这部分工作正常。下一个连接实际上完成了上传。我匹配了浏览器发送的http头和java代码,它们看起来都一样


我已经在这上面呆了很长时间了。提前感谢。

您是否有控制台(命令行)访问实例的权限。然后,您可能可以通过Linux中的netstat之类的工具检查已连接/正在连接的端口。谢谢您的回复。不,我不知道如何访问控制台。通过netstat我们需要什么?您的java代码希望在服务器上打开端口80。netstat将返回试图连接的ip地址:端口。您可以发布upload_url?@Mars(手动编辑的部分)的内容吗 http://.appspot.com/_ah/upload/AMmfu6Zo2ET0UBLnrhd-8SOd0sC5p79MxtnJmepwpeOjixXjM7eelA2Fjo6znpnbRnqpZVGPo-8WwdKsmYb7oavyKPO-Tw4pz2S3HfC_7ztBOmODzrIYUtKkUwT4L5Z0nlziY457aA4XTOtGxK9tg64yWN7tnpB5kucx9dO4EbpyicBkq29WrK8/ALBNUaYAAAAAU9v5PW92CvOv92HdPMLbzTNGFOXTlRDO/