【问题标题】:How upload an image from sd card to a folder in server如何将图像从 sd 卡上传到服务器中的文件夹
【发布时间】:2012-11-22 05:01:14
【问题描述】:

我想将图像从我的 sd 卡上传到服务器中的文件夹。我有以下信息 http://mywebsite.myworks.com/parent/webservices/myuploader.ashx (POST)

Content-Disposition: form-data; name="Filename"
FilName.jpg

Content-Disposition: form-data; name="password"
mypassword

Content-Disposition: form-data; name="action"
upload

Content-Disposition: form-data; name="parentNodeId"
1297

Content-Disposition: form-data; name="replaceExisting"
0

Content-Disposition: form-data; name="username"
admin

Content-Disposition: form-data; name="Filedata"; filename="2012-11-07 11-03-37.jpg"
Content-Type: application/octet-stream
[DATA]

基本上我想要的是将图像上传到文件夹。到目前为止我所了解的是上传到 parentNodeId 为 1297 的文件夹。我看到了一些关于如何将图像上传到 php 服务器的教程。基于我写的一个函数。我在下面分享它

private void doFileUpload() {
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String exsistingFileName = "/sdcard/r.jpg";

    // Is this the place are you doing something wrong.
    String lineEnd = "rn";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    String responseFromServer = "";
    String urlString = "http://mywebsite.myworks.com/parent/webservices/myuploader.ashx";

    try {
        // ------------------ CLIENT REQUEST
        Log.e("UploadMe", "Inside second Method");
        FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));

        // open a URL connection to the Servlet
        URL url = new URL(urlString);

        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();

        // Allow Inputs
        conn.setDoInput(true);

        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        // conn.setRequestProperty("Content-Type",
        // "multipart/form-data;boundary="+boundary);
        conn.setRequestProperty("Content-Type", "application/octet-stream");

        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
         dos.writeBytes("Content-Disposition: form-data; name=\"Filename\";filename=\""
         + exsistingFileName + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"Filename\";filename=\""
                + "r.jpg" + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"password\";filename=\""
                + "mypassword" + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"action\";filename=\""
                + "upload" + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"parentNodeId\";filename=\""
                + "-1" + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"replaceExisting\";filename=\""
                + "0" + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"username\";filename=\""
                + "admin" + "\"" + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"Filedata\";filename=\""
                + "2012-11-07 11-03-37.jpg" + "\"" + lineEnd);

        dos.writeBytes(twoHyphens + boundary + lineEnd);

        dos.writeBytes(lineEnd);
        Log.e("UploadMe", "Headers are written");

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

        // close streams
        Log.e("UploadMe", "File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();

    }

    catch (MalformedURLException ex)

    {

        Log.e("UploadMe", "error: " + ex.getMessage(), ex);

    }

    catch (IOException ioe)

    {

        Log.e("UploadMe", "error: " + ioe.getMessage(), ioe);

    }

    // ------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream(conn.getInputStream());
        String str;

        while ((str = inStream.readLine()) != null) {
            Log.e("UploadMe", "Server Response" + str);
        }

        inStream.close();
    }

    catch (IOException ioex) {
        Log.e("UploadMe", "error: " + ioex.getMessage(), ioex);
    }

}

程序运行没有任何崩溃,我从我的 log cat 输出中得到这些。但我在我的服务器文件夹中看不到任何东西。

11-22 10:26:39.546: E/UploadMe(3363): Inside second Method
11-22 10:26:39.843: E/UploadMe(3363): Headers are written
11-22 10:26:39.851: E/UploadMe(3363): File is written
11-22 10:26:40.820: E/UploadMe(3363): Server Response<?xml version="1.0"?>
11-22 10:26:40.820: E/UploadMe(3363): Server Response<MediaResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" success="false" />

由于我不知道将文件上传到服务器,这是上传文件的方式吗?

【问题讨论】:

标签: android image-uploading


【解决方案1】:

一个可用的库调用android-async-http-1.4.2.jar 使用这个你也可以上传带有帖子字符串数据的文件

这里是示例代码

RequestParams params = new RequestParams();
params.put("fname", "name");// here write your parameter name and its value
params.put("file", new File(filePath)); // Upload a File
// params.put("profile_picture2", someInputStream); // Upload an
// InputStream
// params.put("profile_picture3", new ByteArrayInputStream(someBytes));
// // Upload some bytes

AsyncHttpClient client = new AsyncHttpClient();
client.post(urlServer, params, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String arg0) {
        super.onSuccess(arg0);
        Log.v("from response", arg0);
    }
});

这里是jar下载链接

https://github.com/loopj/android-async-http/downloads

【讨论】:

    【解决方案2】:

    看到这个

    链接how to Upload image into server in Android?

    在此示例中,他们希望与您在服务器上上传图像相同,但存储在文件夹中的是您的 Web 服务开发人员实际存储图像的任务。

    您的任务只是将图像发送到服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-14
      • 2011-09-10
      • 2013-07-25
      • 2014-03-30
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多