程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-上傳圖像到 php 服務器的問題

android-上傳圖像到 php 服務器的問題

編輯:編程綜合問答
上傳圖像到 php 服務器的問題

我不能夠把圖像上傳到php服務器。當查詢的時候,我發現有這部分代碼,不能把圖像上傳到 php 服務器。再次如何把用戶名、密碼、模塊和函數名傳給當前的請求?下面是我在網上找到的代碼

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

String pathToOurFile = "/data/file_to_send.mp3";
String urlServer = "http://192.168.1.1/handle_upload.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)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();

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

看似也不太好用,怎麼修改代碼呢?

最佳回答:


從上面的代碼來看,你可以移除沒有的參數,如果需要的話,以同樣的方式添加別的參數。

public static String UplaodData(byte[] data , String text, String id, boolean personalCheck, boolean privateCheck, String  ImageName, boolean isClaimNote, String url, String percentage, boolean showProgress)
{
    HttpURLConnection connection    = null;
    DataOutputStream outputStream   = null;
    DataInputStream  inputStream    = null;


    //String claimId = ClaimList.claimId;

    String noteType = null;
    String isImageFile = null;

    String Responce                 =  "";
    String urlServer                = url;
    String lineEnd                  = "\r\n";
    String twoHyphens               = "--";
    String boundary                 = "*****";
    System.out.println("Value of note type----------------------- "+noteType);
    try
    {

        URL url1 = new URL(urlServer);
        connection = (HttpURLConnection) url1.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() );
        if (data != null)
        {
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data;  Content-type: image/png; name=\"attachment\"; filename=\""+ImageName+"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.write(data, 0, data.length);
            outputStream.writeBytes(lineEnd);
        }

        if(isClaimNote){
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"claimId\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(id);
            outputStream.writeBytes(lineEnd);
        }else{
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"activityId\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(id);
            outputStream.writeBytes(lineEnd);
            if(showProgress){
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream.writeBytes("Content-Disposition: form-data; name=\"activityResolution\"" + lineEnd);
                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(percentage);
                outputStream.writeBytes(lineEnd);
            }
        }

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"noteType\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(noteType);
        outputStream.writeBytes(lineEnd);

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"noteText\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(text);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"isImageFile\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(isImageFile);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        inputStream  = new DataInputStream( connection.getInputStream());

        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = "";


        while ((serverResponseMessage = inputStream.readLine()) != null)
        {
                Responce   = Responce + serverResponseMessage;
        }

        outputStream.flush();
        outputStream.close();
        return Responce;

        }
        catch (Exception ex)
        {
            Responce   = ex.toString();
            return Responce;
        }
}

上面的代碼是關於上傳多方數據的,根據你的程序中list的參數自定義。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved