【问题标题】:How to POST File to an URL in Java CodeName One?如何将文件发布到 Java CodeName One 中的 URL?
【发布时间】:2019-05-07 17:28:43
【问题描述】:

我想在 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,我想在 URL 中获取类似文件的图像,就像在 Postman 中一样

    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();

}

【问题讨论】:

  • Java Post request with form data 的可能副本看看那个。提交文件时,php 在表单编码方面不是很宽容。在所有情况下,查看您的 symfony 应用程序(邮递员与 java 应用程序)中的内容
  • 感谢您的回答。这些仅带有字符串参数,但我想要一个图像
  • 我就是这么说的。当您要上传文件并且网络服务器正在运行 php 时,它只会接受该文件,当您使用正确的表单编码发送它时(因为该文件仍然是表单提交的一部分,因为您实际上伪造了一个html 表单提交)请参阅 php.net/manual/en/features.file-upload.post-method.php 以获取一些出色的示例,但是该页面上的第二个注释是相关的..

标签: java file symfony web-services codenameone


【解决方案1】:

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

这是不正确的:

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() 等将内容包含在请求中。

【讨论】:

  • 我不明白 x-www-url-form-encoded 是什么意思,昨天我使用了addArgument(),但它没有用
【解决方案2】:

我解决了这个问题,我修改了“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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多