如何以代号名称将图像上传到服务器并取回

问题描述 投票:2回答:1

嗨,我正在尝试使用codenameone从设备上载图像并将其上载到php服务器,并将该图像Url存储在mysql数据库中,然后想取回该图像以在设备上显示,但无法正常工作以下是我的Java代码

jobIcon.addActionListener((ActionEvent en) -> {
           if(Dialog.show("Camera or Gallery", "Would you like to use the camera or the gallery for the picture?", "Camera", "Gallery")) {
             jobPic = Capture.capturePhoto();
            if(jobPic != null) 
                try{
          Image img = Image.createImage(jobPic);
             ImageIO imgIO= ImageIO.getImageIO();
             ByteArrayOutputStream out = new ByteArrayOutputStream();
             imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
           bytesdata = out.toByteArray();
             jobIcon.setIcon(img);

            }catch(IOException err) {
                        ToastBar.showErrorMessage("An error occured while loading the image: " + err);
                        Log.e(err);
                    }
        } else {
            Display.getInstance().openGallery(ee -> {
                if(ee.getSource() != null) {
                    try {
                        jobPic =(String)ee.getSource();
                      Image img = Image.createImage((String)ee.getSource());
                      ImageIO imgIO= ImageIO.getImageIO();
                      ByteArrayOutputStream out = new ByteArrayOutputStream();
                      imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
                      bytesdata = out.toByteArray();
                      jobIcon.setIcon(img);

                    } catch(IOException err) {
                        ToastBar.showErrorMessage("An error occured while loading the image: " + err);
                        Log.e(err);
                    }
                }
            },Display.GALLERY_IMAGE); 

我不想在此之后立即显示图像,如果我删除Display.GALLERY_IMAGE,它会显示错误,然后我通过多部分请求发送数据

  if (jobPic !=null)
             {
               r.addData("jobPic",bytesdata,"image/jpeg" );  
             }

然后使用下面的php脚本处理数据

if (isset ($_FILES["jobPic"])) {
$image = $_FILES['jobPic']['name'];
$target_path = "/uploads/";
$dir = sys_get_temp_dir();
$uid = uniqid();
$file = $uid .$image.".jpeg" ;
move_uploaded_file($_FILES["jobPic"]["tmp_name"],$target_path . $file);

$jobPic =$dir. $target_path . $file;
}else {
    $jobPic = "";

然后存储在数据库中的Url看起来像这样

D:localTemp/uploads/592caa9de79fdjobPic.jpeg

并且从服务器获取URL后,我无法使用代号一个的URL创建该图像

请纠正我错了的地方或建议我使用其他替代方法

java php codenameone
1个回答
0
投票
$ target_path =“ / uploads /”;必须为$ target_path =“ uploads /”;
© www.soinside.com 2019 - 2024. All rights reserved.