2011-11-08 145 views
0

我需要在服務器上傳音頻文件(或任何文件)。我有asp.net服務器,並引用此代碼,但根據我的疑問,這是PHP服務器上傳代碼,但我需要在asp.net中做。那麼適用的變化是什麼?上傳數據麻煩android

一件事是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> 
+0

看看我編輯的答案。希望這會幫助你。 – user370305

回答

0

要上傳服務器上的文件我不這麼認爲,因爲asp.net和PHP服務器有一個di 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腳本。

+0

你能否詳細說明我該如何檢查服務器端腳本? –

+0

還有一件事是我不知道我應該在所有'setRequestProperty'內容中傳遞什麼參數。看我的代碼。 –

+0

還有一件事是URL像這樣::'http:// xyz/MRESC/images/CustomizeActivity/193 /'所以它不存儲在數據庫中它存儲在目錄中。 –

0

我建議你使用上傳到服務器的數據HttpClient。 There are some samples of how to do this但是,如果你的文件很大,你將需要更改服務器實現 - 你的服務器應該可以通過部分獲取文件,否則你的android應用程序將因堆空間而失敗。