我想用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 中添加图像,它起作用了
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/