如何 POST 归档到 URL Java CodeName One?

How to POST File to an URL in Java CodeName One?

我想在 java 中上传图片并复制到 Symfony 中带有 WebService 的目录中 我用 Postman 试过了,它起作用了,但是当我在 Java 中做它时,它不起作用,我不知道如何在 Url 请求中传递像 paramatre 这样的文件 请帮我找到解决办法

Symfony 代码:

    $file = $request->files->get('nomImage');
    $status = array('status' => "success","fileUploaded" => false);

    // If a file was uploaded
    if(!is_null($file)){
        // generate a random name for the file but keep the extension
        $filename = uniqid().".".$file->getClientOriginalExtension();
        $path = "C:\wamp64\www\pidev\web\uploads\images";
        $file->move($path,$filename); // move the file to a path
        $status = array('status' => "success","fileUploaded" => true);
    }

    return new JsonResponse($status);

邮递员截图: 我用 Postman 发送了 URL 并在 Body 中添加图像,其中的 nomImage 是键,图像是值,它起作用了

Java代码: 此代码用于连接到 URL,我想像在 Postman

中一样获取 URL 中的图像文件
    public void ajoutProduit(File image)
    {
    ConnectionRequest con = new ConnectionRequest();
    con.setUrl("http://localhost/PIDEV/web/app_dev.php/Api/produit/ajout?nomImage="+image);
    NetworkManager.getInstance().addToQueueAndWait(con);
    }

这是我的表格和图片的上传并执行它不起作用的图片的副本

public class AjoutProduit {


private Form fAjout = new Form("", new BoxLayout(BoxLayout.Y_AXIS));
public AjoutProduit() {    

    TextField nomProduit = new TextField("", "Nom du produit");
    TextField descProduit = new TextField("", "Description du produit");
    ComboBox<String> opProduit = new ComboBox<>(
            "",
            "echanger",
            "donner",
            "recycler",
            "reparer"
    );

    final String[] jobPic = new String[1];
    Label jobIcon = new Label();

    Button image = new Button("Ajouter une image ");
    final String[] image_name = {""};
    final String[] pathToBeStored={""};

    /////////////////////Upload Image
    image.addActionListener((ActionEvent actionEvent) -> {
    Display.getInstance().openGallery(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ev) {
            if (ev != null && ev.getSource() != null) {
                String filePath = (String) ev.getSource();
                int fileNameIndex = filePath.lastIndexOf("/") + 1;
                String fileName = filePath.substring(fileNameIndex);
                Image img = null;
                try {
                    img = Image.createImage(FileSystemStorage.getInstance().openInputStream(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                image_name[0] = System.currentTimeMillis() + ".jpg";
                jobIcon.setIcon(img);
                System.out.println(filePath);
                System.out.println(image_name[0]);

                try {
                         pathToBeStored[0] = FileSystemStorage.getInstance().getAppHomePath()+ image_name[0];
                        OutputStream os = FileSystemStorage.getInstance().openOutputStream(pathToBeStored[0]);
                        ImageIO.getImageIO().save(img, os, ImageIO.FORMAT_JPEG, 0.9f);
                        os.close();
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
            }
        }
    }, Display.GALLERY_IMAGE);});



            ////////////Copied with URL Symfony
            Button myButton = new Button("Valider");
            myButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    ServiceProduit sp = new ServiceProduit();
                    ServiceEchange se  = new ServiceEchange();
                    String path = "C:/Users/omark/.cn1/"+image_name[0];
                   File file = new File(path);
                   sp.ajoutProduit(file);


                }
            });


    fAjout.addAll(nomProduit,descProduit,opProduit,jobIcon,myButton,image);
    fAjout.show();

}

试试 x-www-url-form-encoded。如果可行,则使用 MultipartRequest 将二进制数据提交到服务器。它隐式地为您处理表单编码提交。如果某些东西不起作用,请使用 Codename One 中的网络监控工具来检查传出 request/response,这通常会提供有关该过程的有用信息。

这是不正确的:

ConnectionRequest con = new ConnectionRequest();
con.setUrl("http://localhost/PIDEV/web/app_dev.php/Api/produit/ajout?nomImage="+image);
NetworkManager.getInstance().addToQueueAndWait(con);

您正在使用 GET 样式参数传递提交 URL。您需要提交图像的日期而不是图像本身。您需要使用 addArgument()addData() 等来包含请求中的内容。

我解决了问题,我修改了“Java代码”:

    MultipartRequest cr = new MultipartRequest();
    cr.setUrl("http://localhost/PIDEV/web/app_dev.php/Api/produit/ajout");
    cr.setPost(true);
    String mime = "image/png";
    try {
        cr.addData("file", filePath, mime);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String fichernom = System.currentTimeMillis() + ".png";
    cr.setFilename("file", fichernom);

    InfiniteProgress prog = new InfiniteProgress();
    Dialog dlg = prog.showInifiniteBlocking();
    cr.setDisposeOnCompletion(dlg);
    NetworkManager.getInstance().addToQueueAndWait(cr);