文件不会从Android的上传到PHP服务器 [英] file is not uploading from android to php server

查看:146
本文介绍了文件不会从Android的上传到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我写了一个code,如下图所示从我的Andr​​oid手机上传文件PHP服务器。在应用程序运行而没有任何错误,但该文件没有上传到服务器。我去哪儿了?

公共类上传延伸活动{

  HttpURLConnection的连接= NULL;
DataOutputStream类的OutputStream = NULL;
DataInputStream以InputStream的= NULL;字符串pathToOurFile =SD卡/ Android的/数据/ file.pdf
字符串urlServer =htt​​p://mpss.csce.uark.edu/~smandava/upload.php;
字符串lineEnd =\\ r \\ n;
串twoHyphens = - ;
字符串边界=*****;
INT读取动作,方bytesAvailable,缓冲区大小;
字节[]缓冲区;
INT MAXBUFFERSIZE = 1 * 1024 * 1024;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    尝试
    {
    的FileInputStream的FileInputStream =新的FileInputStream(新文件(pathToOurFile));    网址URL =新的URL(urlServer);
    连接=(HttpURLConnection类)url.openConnection();    //允许输入和放大器;输出
    connection.setDoInput(真);
    connection.setDoOutput(真);
    connection.setUseCaches(假);    //启用POST方法
    connection.setRequestMethod(POST);    connection.setRequestProperty(连接,保持活动);
    connection.setRequestProperty(内容类型,的multipart / form-data的;边界=+边界);    的OutputStream =新的DataOutputStream类(connection.getOutputStream());
    outputStream.writeBytes(twoHyphens +边界+ lineEnd);
    outputStream.writeBytes(内容处置:表格数据;名称= \\UploadedFile的\\;文件名= \\+ pathToOurFile +\\+ lineEnd);
    outputStream.writeBytes(lineEnd);    参考bytesAvailable = fileInputStream.available();
    BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
    缓冲区=新的字节[缓冲区大小]    //读取文件
    读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);    而(读取动作大于0)
    {
    outputStream.write(缓冲液,0,缓冲区大小);
    参考bytesAvailable = fileInputStream.available();
    BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
    读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
    }    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);    //服务器的响应(code和消息)
    INT serverResponse code = connection.getResponse code();
    串serverResponseMessage = connection.getResponseMessage();    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    赶上(异常前)
    {
    //异常处理
    }
}

}

// upload.php的

  $ target_path =htt​​p://mpss.csce.uark.edu/~smandava/files/;
$ target_path = $ target_path。基本名($ _FILES ['UploadedFile的'] ['名']);
如果(move_uploaded_file($ _ FILES ['UploadedFile的'] ['tmp_name的值'],$ target_path)){
 回声文件。基本名($ _FILES ['UploadedFile的'] ['名'])。
 已上传;
}其他{
 回声发生错误上传文件,请重试!
}


解决方案

在$ target_path应该是一个文件系统路径,而不是一个网站的网址。

  $ target_path ='/家庭/ smandava /的public_html /文件/'; //这样的事情

Hi I wrote a code as shown below for uploading file from my android phone to php server. The application is running without any error, but the file is not uploaded to the server. Where did I go wrong ?

public class upload extends Activity {

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

String pathToOurFile = "sdcard/Android/data/file.pdf";
String urlServer = "http://mpss.csce.uark.edu/~smandava/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    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");

    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();

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


}

}

//upload.php

$target_path = "http://mpss.csce.uark.edu/~smandava/files/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}

解决方案

The $target_path should be a file system path, not a web url.

$target_path = '/home/smandava/public_html/files/'; // something like this

这篇关于文件不会从Android的上传到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆