gpt4 book ai didi

android - 使用 Android HTTP Post 将文件上传到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 00:06:22 25 4
gpt4 key购买 nike

大家好,请帮助我,我已经挣扎了一个多月了!!!我想将图像上传到 MYSQL 数据库,而不是其他任何地方。

到目前为止我尝试了什么:

  • 正在将字节数组上传到数据库=FAIL。(不会在服务器端上传)
  • 连接到 mysql 数据库并以字符串形式上传字符=SUCCESS
  • 将 bytearray 作为字符串上传到 mysql=FAIL。(同样不会在服务器端上传)

请给我任何完整的示例甚至源代码如何将图像上传到 mysql 数据库或者至少如何将指向该图像的链接上传到 mysql 数据库以及服务器端 PHP 代码因为我不知道 php 我只需要通过示例放在服务器端

先谢谢大家了,请帮帮我!

最佳答案

为我工作!UploadtoServer 类中的方法扩展了 Activiy:

  public int uploadFile(String sourceFileUri) {

String lstrArq = SpnListarArquivosCelular.getSelectedItem().toString();
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);

if (!sourceFile.isFile()) {

dialog1.dismiss();

Log.e("uploadFile", "O arquivo não existe :"
+uploadFilePath + "" + lstrArq);

runOnUiThread(new Runnable() {
String lstrArq = SpnListarArquivosCelular.getSelectedItem().toString();
public void run() {
messageText.setText("O arquivo não existe :"
+uploadFilePath + "" + lstrArq);
}
});

return 0;

}
else
{
try {

// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);

// Open a HTTP connection to the URL
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 lstrArq = SpnListarArquivosCelular.getSelectedItem().toString();
String msg = "O arquivo " + lstrArq + " foi enviado com sucesso!";

messageText.setText(msg);
Toast.makeText(UploadToServer.this, "O arquivo " + lstrArq + " foi enviado com sucesso!",
Toast.LENGTH_LONG).show();
}
});
}

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

} catch (MalformedURLException ex) {

dialog1.dismiss();
ex.printStackTrace();

runOnUiThread(new Runnable() {
public void run() {
messageText.setText("MalformedURLException Exception : check script url.");
Toast.makeText(UploadToServer.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
}
});

Log.e("Erro no servidor", "error: " + ex.getMessage(), ex);
} catch (Exception e) {

dialog1.dismiss();
e.printStackTrace();

runOnUiThread(new Runnable() {
public void run() {
messageText.setText("Got Exception : see logcat ");
Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}
dialog1.dismiss();
return serverResponseCode;

} // End else block
}

服务器中的 Cod PHP:

<?php

$file_path = "mypatholder/";

$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";
}
?>

再见!

关于android - 使用 Android HTTP Post 将文件上传到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293490/

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