我在这里有一个非常奇怪的错误,我已成功上传我的网络服务器中的图像,我的意思是它显示在那里,但是当我尝试打开它时,它说“在此服务器上找不到请求的URL xxxx” 。这是它的ss:
这里的文件显然在服务器上:
以下是我的Android代码,以备您需要时使用:
public void uploadimage(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image From Gallery"), 1);
}
@Override
protected void onActivityResult(int RC, int RQC, Intent I) {
super.onActivityResult(RC, RQC, I);
if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri = I.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
profile_pic.setImageBitmap(bitmap);
ImageUploadToServerFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void ImageUploadToServerFunction(){
created=false;
ByteArrayOutputStream byteArrayOutputStreamObject ;
byteArrayOutputStreamObject = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStreamObject);
byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();
long lengthbmp = byteArrayVar.length;
final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);
class AsyncTaskUploadClass extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(AccountInfoActivity.this, "Image is Uploading", "Please Wait", false, false);
}
@Override
protected void onPostExecute(String string1) {
super.onPostExecute(string1);
// Dismiss the progress dialog after done uploading.
progressDialog.dismiss();
// Printing uploading success message coming from server on android app.
Toast.makeText(AccountInfoActivity.this, string1, Toast.LENGTH_LONG).show();
profile_pic.setImageBitmap(bitmap);
// Setting image as transparent after done uploading.
}
@Override
protected String doInBackground(Void... params) {
ImageProcessClass imageProcessClass = new ImageProcessClass();
HashMap<String, String> HashMapParams = new HashMap<String, String>();
HashMapParams.put(ImageName, accessSharedPreferences.getFullName(AccountInfoActivity.this)+"-1234567890");
HashMapParams.put(ImagePath, ConvertImage);
String FinalData = imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
return FinalData;
}
}
AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
AsyncTaskUploadClassOBJ.execute();
}
public class ImageProcessClass {
public String ImageHttpRequest(String requestURL, HashMap<String, String> PData) {
StringBuilder stringBuilder = new StringBuilder();
try {
URL url;
HttpURLConnection httpURLConnectionObject;
OutputStream OutPutStream;
BufferedWriter bufferedWriterObject;
BufferedReader bufferedReaderObject;
int RC;
url = new URL(requestURL);
httpURLConnectionObject = (HttpURLConnection) url.openConnection();
httpURLConnectionObject.setReadTimeout(19000);
httpURLConnectionObject.setConnectTimeout(19000);
httpURLConnectionObject.setRequestMethod("POST");
httpURLConnectionObject.setDoInput(true);
httpURLConnectionObject.setDoOutput(true);
OutPutStream = httpURLConnectionObject.getOutputStream();
bufferedWriterObject = new BufferedWriter(
new OutputStreamWriter(OutPutStream, "UTF-8"));
bufferedWriterObject.write(bufferedWriterDataFN(PData));
bufferedWriterObject.flush();
bufferedWriterObject.close();
OutPutStream.close();
RC = httpURLConnectionObject.getResponseCode();
if (RC == HttpsURLConnection.HTTP_OK) {
bufferedReaderObject = new BufferedReader(new InputStreamReader(httpURLConnectionObject.getInputStream()));
stringBuilder = new StringBuilder();
String RC2;
while ((RC2 = bufferedReaderObject.readLine()) != null) {
stringBuilder.append(RC2);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private String bufferedWriterDataFN(HashMap<String, String> HashMapParams) throws UnsupportedEncodingException {
StringBuilder stringBuilderObject;
stringBuilderObject = new StringBuilder();
for (Map.Entry<String, String> KEY : HashMapParams.entrySet()) {
if (check)
check = false;
else
stringBuilderObject.append("&");
stringBuilderObject.append(Uri.encode(KEY.getKey(), "UTF-8"));
stringBuilderObject.append("=");
stringBuilderObject.append(Uri.encode(KEY.getValue(), "UTF-8"));
}
return stringBuilderObject.toString();
}
}
用于上传的PHP代码:
<?php
// Create connection
$conn = mysqli_connect("x.x.x.x", "xxxxx", "xxxx", "xxxxx");
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$DefaultId = 0;
$ImageData = $_POST['image_path'];
$ImageName = $_POST['image_name'];
$GetOldIdSQL ="SELECT id FROM UploadImageToServer ORDER BY id ASC";
$Query = mysqli_query($conn,$GetOldIdSQL);
while($row = mysqli_fetch_array($Query)){
$DefaultId = $row['id'];
}
$ImagePath = "images/$ImageName.jpg";
$ServerURL = "http://raymartsison.x10host.com/uploads/$ImagePath";
$InsertSQL = "insert into UploadImageToServer (image_path,image_name) values ('$ServerURL','$ImageName')";
if(mysqli_query($conn, $InsertSQL)){
file_put_contents($ImagePath,base64_decode($ImageData));
echo "Your Image Has Been Uploaded.";
}
mysqli_close($conn);
}else{
echo "Not Uploaded";
}
?>
我认为这很奇怪,因为屏幕截图上的第一个文件(“Aw aw-1234567890.jpg”)完美无缺,但其他人却没有。另一个原因是当我尝试重命名或更改那些奇怪文件的扩展名时,错误消失了,我可以打开它们。 任何帮助将非常感谢。 编辑: 不工作图片的网址: http://raymartsison.x10host.com/uploads/images/se%20se-1234567890.jpg
网址工作(重命名): http://raymartsison.x10host.com/uploads/images/se%20se-123456789.jpg