【发布时间】:2020-11-08 23:46:47
【问题描述】:
我正在尝试使用 android-studio 和 php 将图像上传到 xampp 服务器 但我不会成功 请帮帮我
这是我的代码(java 和 php):
java代码: 在这段代码中,我试图从图库中选择图片并显示一个对话框,然后上传图片...
public final int REQUEST_OPEN_GALLERY = 5;
ProgressDialog dialog;
Thread uploadThread;
String lineEnd = "\r\n";
String twoHyphens = "--";
String id = "";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
public static Handler uploadHandler;
public static String imageProfile = "";
profile_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseFile();
}
});
}
private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_OPEN_GALLERY);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OPEN_GALLERY && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
String[] info = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, info, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(info[0]);
final String filePath = cursor.getString(columnIndex);
//Toast.makeText(MainActivity.this,filePath,Toast.LENGTH_SHORT).show();
dialog = ProgressDialog.show(getContext(), "upload", "Uploading file...");
uploadThread = new Thread(new Runnable() {
@Override
public void run() {
uploadFile(filePath);
}
});
uploadThread.start();
}
}
private void uploadFile(String filePath) {
File file = new File(filePath);
try {
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://192.168.1.6/zanborak/sabtenam/upload.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("uploaded_file", filePath);
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd + "Content-Disposition: form-data;" +
" name=\"uploaded_file\";filename=\"" + filePath + "\"\r\n\r\n");
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
if (connection.getResponseCode() == 200) {
uploadHandler.post(new Runnable() {
@Override
public void run() {
dialog.dismiss();
Toast.makeText(getContext(), "Upload Completed", Toast.LENGTH_SHORT).show();
imageProfile = "";
}
});
}
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
和php:
在此代码中,我尝试将图像移动到“上传”文件夹...
<?php
include "connect.php";
$rand=microtime();
$path="upload/".$rand.($_FILES["uploaded_file"]["name"]);
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],$path);
?>
【问题讨论】:
标签: java php android-studio xampp