2 znl 12 znl_12 于 2013.01.21 16:11 提问

上传图像到 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
}

看似也不太好用,怎么修改代码呢?

1个回答

Baby_Bonnie
Baby_Bonnie   2013.01.21 17:29
已采纳

从上面的代码来看,你可以移除没有的参数,如果需要的话,以同样的方式添加别的参数。

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的参数自定义。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!