gpt4 book ai didi

java - 我将图像存储在数据库中但已被替换

转载 作者:行者123 更新时间:2023-11-30 22:14:23 25 4
gpt4 key购买 nike

我在这里使用 volley 运行简单的代码,该代码只是将从我的手机拍摄的照片上传到我的网络服务器中的数据库。

这是我的主要 activity.java:

public class MainActivity extends AppCompatActivity {

private Button button;
private String encoded_string, image_name;
private Bitmap bitmap;
private File file;
private Uri file_uri;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.start);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUri();
i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
startActivityForResult(i, 10);
}
});
}

private void getFileUri() {
image_name = "testing123.jpg";

file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + image_name
);

file_uri = Uri.fromFile(file);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 10 && resultCode == RESULT_OK) {
new Encode_image().execute();
}
}

private class Encode_image extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {

bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
bitmap.recycle();

byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array, 0);
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
makeRequest();
}
}

private void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, "http://mysite/connection.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("encoded_string",encoded_string);
map.put("image_name",image_name);

return map;
}
};
requestQueue.add(request);
}
}

好的,我现在有这个问题,当我上传图片时,我知道它会被上传到我的网络服务器中的图像文件夹,但它会上传名称为 testing123.jpg 的图像,如果我采取它存储到数据库中的另一张照片,但在我的图像文件夹中,它不会创建,而是用新图像替换我现有的 testing123.jpg,我想要的是当我拍照时它存储照片但名称不同,或名称手机自己提供的照片,我知道错误发生在这里

 private void getFileUri() {
image_name = "testing123.jpg";

file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + image_name
);

file_uri = Uri.fromFile(file);
}

我的connection.php

<?php
header('Content-type : bitmap; charset=utf-8');

if(isset($_POST["encoded_string"])){

$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];

$decoded_string = base64_decode($encoded_string);

$path = 'images/'.$image_name;

$file = fopen($path, 'wb');

$is_written = fwrite($file, $decoded_string);
fclose($file);

if($is_written > 0) {

$connection = mysqli_connect('mysql.host', 'user', 'pass','db');
$query = "INSERT INTO photos(name,path) values('$image_name','$path');";

$result = mysqli_query($connection, $query) ;

if($result){
echo "success";
}else{
echo "failed";
}

mysqli_close($connection);
}
}
?>

好的,这就是第一个问题

关于我的另一个问题只是安全问题

假设我使用我的脚本 connect.php 上传了我的照片,但我不希望其他人通过我的页面与我的脚本交互,假设我只想使连接私密和安全,我应该怎么做?使 REST API 像严肃的应用程序一样更安全地处理连接?

最佳答案

最大的问题是你不能根据用户给你的名字保存图像,这是一个巨大的安全问题,更不用说它会让一个人随意破坏你所有的图像。您需要为每次上传生成一个随机名称,或基于您的 INSERT(例如 LAST_INSERT_ID())生成的记录 ID,只要满足以下条件,该名称就应该是唯一的您永远不会重置 PRIMARY KEY 序列。

还值得注意的是,HTTP 分段上传是发送任意文件数据的标准方式。如果有人可以发送无限大小的有效载荷,那么您就是在将自己暴露在 DDOS 攻击之下。

关于java - 我将图像存储在数据库中但已被替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946394/

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