gpt4 book ai didi

java - 如何在httpurlconnection android中添加参数

转载 作者:太空宇宙 更新时间:2023-11-04 14:26:35 25 4
gpt4 key购买 nike

下面的代码成功将图像发送到服务器。

但是我需要的是向数据库添加图像描述和图像日期等参数。

我不知道如何在 HttURLconnection 中添加参数。

    String fileName = sourceFileUri;

HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);


FileInputStream fileInputStream = new FileInputStream(sourceFile_imagepath);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
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);

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

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

Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);

if (serverResponseCode == 200) {

runOnUiThread(new Runnable() {
public void run() {
String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
+ " c:/wamp/www/echo/uploads";
messageText.setText(msg);
Toast.makeText(MainActivity.this,
"File Upload Complete.", Toast.LENGTH_SHORT)
.show();
}
});
}

// close the streams //
fileInputStream.close();
dos.flush();
dos.close();

和 PHP 代码:

<?php
error_reporting(0);
$file_path = "uploads/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
?>

上面的代码可以很好地将代码上传到服务器。

必须将图像描述存储在数据库中。

如何向上述代码添加参数。

以及如何在php服务器端接收它。

任何相关教程或文档。

提前致谢。

最佳答案

检查这个link

Android 代码:

字符串参数=“值”;

        dos.writeBytes("Content-Disposition: form-data; name=\"parameter\"" + lineEnd); 
//dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
//dos.writeBytes("Content-Length: " + parameter.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(parameter); // mobile_no is String variable
dos.writeBytes(lineEnd);

PHP 代码:

$parameter =$_POST['parameter'];

关于java - 如何在httpurlconnection android中添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26608433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com