Android,http:如何将文件上传到共享服务器托管的站点?

发布于 2024-10-11 01:52:13 字数 2606 浏览 3 评论 0原文

我给自己买了一个网站,它托管在一台 Linux 服务器上,该服务器使用 cpanel 共享一个 IP 地址。现在的问题是,我想使用此代码将文件上传到我的网站。每次我使用像 www.site 这样的网站地址时,我都会收到一个异常,提示 URL 格式错误。当我使用 IP 地址时(因为这是共享服务器),我找不到我的 php 代码,因为我不知道如何链接到我的地址。

有谁...有人知道如何将我链接到我的网站,这样我就可以上传 xml 文件吗?这里真的需要帮助......

任何帮助将非常感激,因为我对网络知识一无所知。

    HttpURLConnection connection = null; 
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String pathToOurFile = "/data/data/test.send/testsend.txt";
    String urlServer = "http://www.site.com/filefortransfer.php";
    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");

    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)
    String serverResponseCode = Integer.toString(connection.getResponseCode());
    String serverResponseMessage = connection.getResponseMessage();

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();

    }
    catch (Exception ex)
    {
        Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
    }
    }

I bought myself a website where it is being hosted on a linux server which shares one IP address using cpanel. The problem that is, now, i want to use this code to upload a file to my site. Everytime i use the site address like www.site, i'll get an exception saying that the URL is malformed. When i use the ip address (since this is a shared server), i can't find my php code since i don't know how to link to my address.

Does anyone... anyone know how to link me to my site, just so i can upload an xml file?? Really need help here.......

Any help would be really appreciated as i have no knowledge in networking stuff.

    HttpURLConnection connection = null; 
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String pathToOurFile = "/data/data/test.send/testsend.txt";
    String urlServer = "http://www.site.com/filefortransfer.php";
    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");

    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)
    String serverResponseCode = Integer.toString(connection.getResponseCode());
    String serverResponseMessage = connection.getResponseMessage();

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();

    }
    catch (Exception ex)
    {
        Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-10-18 01:52:14

在 Android 上,更复杂的 HTTP 网络最好使用 HttpClient 类。

以下是分段文件上传的示例: http://rapidandroid.org/wiki/HttpUpload

On Android it's preferable for more complex HTTP networking to use HttpClient class.

Here is an example of multipart file upload: http://rapidandroid.org/wiki/HttpUpload

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文