java - 如何在 Java CodeName One 中将文件 POST 到 URL?

标签 java file symfony web-services codenameone

我想用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 截图: 我使用 Postman 发送了 URL,并使用 nomImage 之类的键和图像之类的值在 Body 中添加图像,它起作用了

enter image description here

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 中的网络监控工具来检查传出的请求/响应,这通常会提供有关该过程的有用信息。

这是不正确的:

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 - 如何在 Java CodeName One 中将文件 POST 到 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55985637/

相关文章:

java - 从 Java 代码调试 Android 应用程序

.net - 如何使 .net 程序集文件尽可能小?

PHP - 如何测试/检查目录/文件夹是否包含旧文件/文件早于?

python - 是否有一个统一的python库来使用不同的协议(protocol)传输文件

Java正则表达式考虑空值

java - Apache-poi 模板前导零在编辑单元格时消失

php - 如何在Symfony 4中使用dataTables防止错误 “Invalid JSON response”?

php - Symfony2 自动加载器找到文件但没有找到类

php - Symfony2 - 如何验证自动完成实体表单类型?

java - 构图 : How to make a shape's dimensions fit around an image?