Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.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
Php 使用HTTP POST将图像上传到Android中的服务器;服务器似乎没有收到映像_Php_Android_Image_Http_Post - Fatal编程技术网

Php 使用HTTP POST将图像上传到Android中的服务器;服务器似乎没有收到映像

Php 使用HTTP POST将图像上传到Android中的服务器;服务器似乎没有收到映像,php,android,image,http,post,Php,Android,Image,Http,Post,我正在尝试将一个图像文件从Android的SD存储上传到服务器。我使用的代码如下,但是在检查服务器接收到的数据时,服务器验证通信是否成功,但不显示数据已发布 private class postData extends AsyncTask<String, Integer, Void> { protected void doInBackground() { HttpURLConnection connection = null; DataOutputStre

我正在尝试将一个图像文件从Android的SD存储上传到服务器。我使用的代码如下,但是在检查服务器接收到的数据时,服务器验证通信是否成功,但不显示数据已发布

private class postData extends AsyncTask<String, Integer, Void> {
     protected void doInBackground() {

    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = "/sdcard/confirmed.jpg";
    String urlServer = "irrelevant";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
        FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");
        System.out.println("posting");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

        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(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

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

        System.out.println(serverResponseCode+serverResponseMessage);

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
    }
    catch (Exception ex)
    {
    //Exception handling
    }
    }

    protected void onProgressUpdate() {
        //called when the background task makes any progress
     }

      protected void onPreExecute() {
         //called before doInBackground() is started
     }
     protected void onPostExecute() {
         //called after doInBackground() has finished 
     }

    @Override
    protected Void doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }
} 
private类postData扩展异步任务{
受保护的void doInBackground(){
HttpURLConnection=null;
DataOutputStream outputStream=null;
DataInputStream inputStream=null;
字符串pathToOurFile=“/sdcard/confirm.jpg”;
字符串urlServer=“不相关”;
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
int字节读取,字节可用,缓冲区大小;
字节[]缓冲区;
int maxBufferSize=1*1024*1024;
尝试
{
FileInputStream FileInputStream=newfileinputstream(新文件(pathToOurFile));
URL=新URL(URL服务器);
connection=(HttpURLConnection)url.openConnection();
//允许输入和输出
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
//启用POST方法
connection.setRequestMethod(“POST”);
系统输出打印项次(“过账”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
outputStream=新的DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(两个连字符+边界+行结束);
outputStream.writeBytes(“内容处置:表单数据;名称=\“uploadedfile\”文件名=\“”+pathToOurFile+“\”“+lineEnd”);
outputStream.writeBytes(lineEnd);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
buffer=新字节[bufferSize];
//读取文件
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
而(字节读取>0)
{
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(两个连字符+边界+两个连字符+lineEnd);
//来自服务器的响应(代码和消息)
int serverResponseCode=connection.getResponseCode();
字符串serverResponseMessage=connection.getResponseMessage();
System.out.println(serverResponseCode+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
捕获(例外情况除外)
{
//异常处理
}
}
受保护的void onProgressUpdate(){
//在后台任务取得任何进展时调用
}
受保护的void onPreExecute(){
//在启动doInBackground()之前调用
}
受保护的void onPostExecute(){
//在doInBackground()完成后调用
}
@凌驾
受保护的Void doInBackground(字符串…arg0){
//TODO自动生成的方法存根
返回null;
}
} 

我很难给出具体细节,但如果需要,请询问,我会尽力给出。

我的代码与您通过Java发布文件的代码几乎相同。我能看到的唯一两个区别是,行端使用的是
\r\n
,而我使用的是
\n
,在传递文件名“pathToOurFile”时,可能传递的是路径,而不仅仅是名称。如果这些更改都不起作用,我将假定服务器工作不正常。您如何访问服务器上的文件?与以下代码类似:
访问调试url时,$\u文件的内容应可见。我将测试线端,但我肯定它应该是一条正在通过的路径。这是调试url,您应该看到一个空数组作为第一个条目,这是我的数据应该出现的地方。