【发布时间】: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