Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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 我总是得到服务器响应ok 200,即使我输入了错误的密码,并且文件没有保存在服务器上_Java_Php_Android_Http Upload - Fatal编程技术网

Java 我总是得到服务器响应ok 200,即使我输入了错误的密码,并且文件没有保存在服务器上

Java 我总是得到服务器响应ok 200,即使我输入了错误的密码,并且文件没有保存在服务器上,java,php,android,http-upload,Java,Php,Android,Http Upload,我正在用Android应用程序中的Java类将文件上载到服务器。 我正在使用一个简单的php脚本来检查密码。 如果我输入了错误的密码,文件不会保存在服务器上,我应该得到403,但我从服务器上得到OK 200 下面是Java类 class httpUploadFile { private int serverResponseCode = 0; int uploadFile(String upLoadServerUri, String uploadFilePath, String

我正在用Android应用程序中的Java类将文件上载到服务器。 我正在使用一个简单的php脚本来检查密码。 如果我输入了错误的密码,文件不会保存在服务器上,我应该得到403,但我从服务器上得到OK 200

下面是Java类

class httpUploadFile {
    private int serverResponseCode = 0;
     int uploadFile(String upLoadServerUri, String uploadFilePath, String uploadFileName,String pfad) {
            String sourceFileUri=uploadFilePath + "" + uploadFileName;
         HttpURLConnection conn;
            DataOutputStream dos;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1024 * 1024;
            File sourceFile = new File(sourceFileUri);
         if (!sourceFile.isFile()) {
             Log.e("uploadFile", "Source File not exist :"
                     +uploadFilePath + "" + uploadFileName);
             return 0;
         }
            try {
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                String fulluri=getUrl(upLoadServerUri,pfad);
                URL url = new URL(fulluri);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("uploaded_file", sourceFileUri);
                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                                + sourceFileUri + "\"" + 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) {
                    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);
                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();
                Log.i("uploadFile", "HTTP Response is : "
                        + serverResponseMessage + ": " + serverResponseCode);
                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (MalformedURLException ex) {

                ex.printStackTrace();

                  Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
            } catch (Exception e) {

                e.printStackTrace();

                Log.e("Upload file Exception", "Exception : "
                        + e.getMessage(), e);
            }

            return serverResponseCode;
        }
    private String getUrl(String BASE_URL,String pfad) {
        String token = getToken();
        String key = getKey(token);
        return String.format("%s?token=%s&key=%s&pfad=%s&", BASE_URL, token, key,pfad);
    }

    private String getKey(String token) {
        return md5(String.format("%s+%s", "wrongpassword", token));
    }

    private String getToken() {
        return md5(UUID.randomUUID().toString());
    }

    private static String md5(String s) {
        MessageDigest m = null;
        try {
            m = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        assert m != null;
        m.update(s.getBytes(), 0, s.length());
        return new BigInteger(1, m.digest()).toString(16);
    }
    }
这里是PHP

<?php
$shared_secret = "password";    
$key = $_GET['key'];    
$token = $_GET['token'];    
$pfad = $_GET['pfad'];    
if ($key != hash("md5", "{$shared_secret}+{$token}")) {    
  header('HTTP/1.0 403 Forbidden');    
  die('403 Forbidden: You are not allowed to access this file.');
}    
$file_path = "/home/www/data/".$pfad."/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else {
    echo "fail";
}
?>

HTTP
200
意味着在HTTP级别上传输是正常的,也就是说,请求在技术上是正常的,服务器能够正确响应

200
不能判断您的业务逻辑是真是假,所以即使密码是错的,只有在服务器和客户端之间的http通信正常的情况下,200才会返回

通常,如果服务器上发生技术问题或无法恢复的问题,我们会使用HTTP5xx进行响应。如果传入请求有问题(例如参数错误),则使用HTTP4xx


您的后端服务器应该做上述判断。

HTTP
200
意味着在HTTP级别上传输是正常的,也就是说,请求在技术上是正常的,并且服务器能够正确响应

200
不能判断您的业务逻辑是真是假,所以即使密码是错的,只有在服务器和客户端之间的http通信正常的情况下,200才会返回

通常,如果服务器上发生技术问题或无法恢复的问题,我们会使用HTTP5xx进行响应。如果传入请求有问题(例如参数错误),则使用HTTP4xx


您的后端服务器应该做上述判断。

403验证函数的If()是否按预期工作?403验证函数的If()是否按预期工作?现在我正在从PHP读取回音,如果我没有获得“成功”,我正在寻找“成功”BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getInputStream());while((line=br.readLine())!=null){response+=line;现在我正在从PHP读取回音,如果我没有得到“success”,我正在寻找“success”,如果我没有得到“success”,那么我就要寻找“success”了BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));while((line=br.readLine())!=null){response+=line;