将jpeg图像发布到PHP服务器:文件已保存但未识别为jpeg图像

将jpeg图像发布到PHP服务器:文件已保存但未识别为jpeg图像,php,android,jpeg,httpurlconnection,Php,Android,Jpeg,Httpurlconnection,我有下面的代码将jpeg文件发布到服务器。问题:文件保存在服务器中,但不被视为jpeg图像。我知道这一点,因为我已经运行了$file foobar.jpg,我只得到了foobar.jpg:data。如果文件是有效的jpeg图像,则该命令将显示如下内容:foobar.jpg:jpeg图像数据,EXIF标准。此外,当我试图用GIMP打开图像时,我得到了一个错误 //FileInputStream fis = null; FileInputStream fileInpu

我有下面的代码将
jpeg
文件发布到服务器。问题:文件保存在服务器中,但不被视为
jpeg
图像。我知道这一点,因为我已经运行了
$file foobar.jpg
,我只得到了
foobar.jpg:data
。如果文件是有效的
jpeg
图像,则该命令将显示如下内容:
foobar.jpg:jpeg图像数据,EXIF标准
。此外,当我试图用GIMP打开图像时,我得到了一个错误

        //FileInputStream fis = null;
        FileInputStream fileInputStream = new FileInputStream(new File(attachmentFileName));


        url = new URL("*****");

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);

        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        //urlConnection.setRequestMethod("GET");
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("Cache-Control", "no-cache");
        urlConnection.setRequestProperty(
                "Content-Type", "multipart/form-data;boundary=" + this.boundary);
                //"Content-Type", "image/jpeg");


        DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream());
        String first = this.twoHyphens + this.boundary + this.crlf;
        String second = "Content-Disposition: form-data; " +
                "name=\"foobar\";" +
                "filename=\"" + attachmentFileName + "\"" +
                "\"\r\n" +
                "Content-type: image/jpeg" +
                "\"\r\n" +
                "Content-Transfer-Encoding: 8bit" +
                "\"\r\n";
        //DATAOUTPUTSTREAM - DATAOUTPUTSTREAM - DATAOUTPUTSTREAM
        DataOutputStream request = new DataOutputStream(urlConnection.getOutputStream());
        request.writeBytes(first); //"--*****\r\n"
        request.writeBytes(second);


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

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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


            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes("\r\n");
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + "\r\n");

        //Log.d(outputStream);

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

        System.out.println(serverResponseCode+serverResponseMessage);

        InputStream is = null;
        try {
            is = urlConnection.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }


        String contentAsString;

        // Convert the InputStream into a string
        try {
            contentAsString = readIt(is, len);

        } catch (IOException e) {
            e.printStackTrace();
        }

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        urlConnection.disconnect();
以下是PHP服务器上的代码:

  $_FILES['foobar'];
  move_uploaded_file($_FILES['foobar']['tmp_name'], 'images/foobar.jpg');


    var_dump($_FILES['foobar']);

    var_dump(move_uploaded_file($_FILES['foobar']['tmp_name'], 'images/jander.jpg'));
这就是价值

--*****
Content-Disposition: form-data; name="foobar";filename="/mnt/sdcard/Pictures/UltimateCameraGuideApp/IMG_20151228_141433.jpg""
Content-type: image/jpeg"
Content-Transfer-Encoding: 8bit"
����)CExif����MM��*����������������\������n��������\�������������������&������������.(�����������������������������i������������6�%���������������� HTC����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������HTC One S����������������������������������������������������������������������������������������������������������������������������������������������������������������������������H������������H���������'����������l���������������0220�������������̐������������������������  �����������������
�����������������������0100�������������������������������������������������������������������2015:12:28 14:14:33��2015:12:28 14:14:33������k������d������������R98������������0100������������������������������������������������n������������v(��������������������������~������������&���������������H������������H���������������      

 $.' ",#(7),01444'9=82<.342     

我会使用一个更简单的HTTP客户端API,比如“this是contentString变量的值:”。胡说没有要开始的内容字符串。而且该数组不会告诉您有关文件内容的任何信息。“[“size”]=>int(239136)”。您的文件大小正确吗?“搬家”后有多少人?为什么移动文件要两次?@greenapps是的,大小正确。我已经编辑了你提到的内容。你编辑了什么?我看不出有什么补充。”t不被识别为jpeg图像,也就是说,在LInux$file foobar.jpg上运行后,我只得到foobar.jpg:data。这句话我一个字也听不懂。请先解释一下你的意思。同时你也可以告诉我们移动的文件包含多少字节。
array(5) {
  ["name"]=>
  string(23) "IMG_20151228_141433.jpg"
  ["type"]=>
  string(11) "image/jpeg""
  ["tmp_name"]=>
  string(14) "/tmp/phpIztN2z"
  ["error"]=>
  int(0)
  ["size"]=>
  int(239136)
}