【问题标题】:trouble in upload data androidandroid上传数据的麻烦
【发布时间】:2012-06-20 15:16:20
【问题描述】:

我需要在服务器中上传音频文件(或任何文件)。我有 asp.net 服务器并引用此代码,但根据我的怀疑,它是 PHP 服务器上传的代码。但我需要在 asp.net 中执行。那么要应用哪些更改?

还有一件事是 url liook 像这样 ::http://xyz/MRESC/images/CustomizeActivity/193/ 所以它不存储在数据库中它存储在目录中

更新 ::

package com.upload;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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

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

     //String pathToOurFile = "/sdcard/audiometer/shanesh1599870.mp3";
     String pathToOurFile = "http://www.deviantart.com/download/78789749/Gohan_Jr__by_android_1.jpg";
     //String urlServer = "http://asd/MRESC/images/CustomizeActivity/193/";
     upLoad2Server(pathToOurFile);
 }



 public static int upLoad2Server(String sourceFileUri) {
      String upLoadServerUri = "http://xyz/MRESC/images/CustomizeActivity/193/";
      // String [] string = sourceFileUri;
      String fileName = sourceFileUri;

      HttpURLConnection conn = null;
      DataOutputStream dos = null;
      DataInputStream inStream = null;
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
      String responseFromServer = "";

      File sourceFile = new File(sourceFileUri);
      if (!sourceFile.isFile()) {
       return 0;
      }
      int serverResponseCode = 0;
    try { // open a URL connection to the Servlet
       FileInputStream fileInputStream = new FileInputStream(sourceFile);
       URL url = new URL(upLoadServerUri);
       conn = (HttpURLConnection) url.openConnection(); // Open a HTTP  connection to  the URL
       conn.setDoInput(true); // Allow Inputs
       conn.setDoOutput(true); // Allow Outputs
       conn.setUseCaches(false); // Don't use a Cached Copy
       conn.setRequestMethod("POST");
       conn.setRequestProperty("Connection", "Keep-Alive");
       conn.setRequestProperty("ENCTYPE", "multipart/form-data");
       conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
       conn.setRequestProperty("uploaded_file", fileName);
       dos = new DataOutputStream(conn.getOutputStream());

       dos.writeBytes(twoHyphens + boundary + lineEnd);
       dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
       dos.writeBytes(lineEnd);

       bytesAvailable = fileInputStream.available(); // create a buffer of  maximum size
       Log.i("Huzza", "Initial .available : " + bytesAvailable);

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

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

       Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
       // close streams
       Log.i("Upload file to server", fileName + " File is written");
       fileInputStream.close();
       dos.flush();
       dos.close();
      } catch (MalformedURLException ex) {
       ex.printStackTrace();
       Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
      } catch (Exception e) {
       e.printStackTrace();
      }
    //this block will give the response of upload link
      try {
       BufferedReader rd = new BufferedReader(new InputStreamReader(conn
         .getInputStream()));
       String line;
       while ((line = rd.readLine()) != null) {
        Log.i("Huzza", "RES Message: " + line);
       }
       rd.close();
      } catch (IOException ioex) {
       Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
      }
      return serverResponseCode;  // like 200 (Ok)

     } // end upLoad2Server


}

权限::

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

【问题讨论】:

  • 看看我编辑的答案。希望这会对你有所帮助。

标签: android


【解决方案1】:

要在服务器上上传文件,我不认为 asp .net 和 PHP 服务器在 android 上有不同的代码。

Just check your server side script. For android both are treated as a server (either a asp .net or PHP). 

我不知道 PHP 或 asp .net,但看看这些例子,

  1. How to upload a file using Java HttpClient library working with PHP - strange problem

  2. HTTP Post multipart file upload in Java ME

  3. Upload image using POST, php and Android

  4. Java Swing File upload with Php on the server

  5. http://www.tizag.com/phpT/fileupload.php

在这些中,有一些 java swing 或 java ME 的示例,但我认为只需使用从 java 上传文件的逻辑,然后看看它们如何使用 php 脚本在服务器端处理。

【讨论】:

  • 你能详细说明我如何检查服务器端脚本吗?
  • 还有一件事是我不知道应该在所有setRequestProperty 内容中传递什么参数。查看我的代码。
  • 还有一件事是 url 看起来像这样 :: http://xyz/MRESC/images/CustomizeActivity/193/ 所以它不存储在数据库中它存储在目录中。
  • 在我的例子中,我将图像放在 MultipartEntity 中设置为文件体,然后在 URL 中给出服务器的 php 脚本的 url 路径。
猜你喜欢
  • 1970-01-01
  • 2017-04-02
  • 2011-09-30
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多