如何在角度和重复的同一请求中上传图像和数据? [英] How can I upload image and data in the same request with angular and resteasy?
问题描述
我正面临一个问题,即在后端使用jax-rs制作角度问题。
crud非常简单,一些文本字段和一个图像字段。
I'm facing a problem to make a angular crud with jax-rs on backend. The crud is very simple, some text fields and a image field.
我有代码正在上传图片:
I have the code working to upload a image:
@POST
@Consumes("multipart/form-data")
public Response uploadFile(MultipartFormDataInput input) {
...
}
在html图层中:
<form action="http://localhost:8080/app/api/user" method="post" enctype="multipart/form-data">
<p>
Choose a file : <input type="file" name="file" />
</p>
<input type="submit" value="Upload" />
</form>
所以,我的问题是如何在这一步中这样做:
So, my question is how can I do this in one step like this:
@POST
@Consumes("multipart/form-data")
public Response save(MultipartFormDataInput input, MyEntity entity) {
...
}
如果我尝试调用上面的代码从视图层,wildfly给出一个错误,找不到要与MyEntity参数绑定的数据。
If I try to call the code above from view layer, the wildfly give a error that doesn't found data to bind with MyEntity parameter.
[org.jboss.resteasy.core.ExceptionHandler] (default task-3) failed to execute: javax.ws.rs.NotSupportedException:
Could not find message body reader for type: class mypackage.MyEntity of content type: multipart/form-data;boundary=----WebKitFormBoundaryRXVvqLpZACPylNgS
有谁知道我该怎么做?或者我应该分两步完成它?
Does anyone knows how can I do that? Or shoud I do it in two steps?
推荐答案
从技术上讲,你可以从<$ c $获得两个数据C> MultipartFormDataInput 。例如
Technically, you can just get both pieces of data from the MultipartFormDataInput
. For example
<form action="api/upload" method="post" enctype="multipart/form-data">
Choose a file : <input type="file" name="file" />
First name: <input type="text" name="firstname" />
List name: <input type="text" name="lastname" />
<input type="submit" value="Upload" />
</form>
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(MultipartFormDataInput multipart) throws IOException {
try (InputStream in = multipart.getFormDataPart("file", InputStream.class, null);
FileOutputStream fos = new FileOutputStream("file.png")) {
byte[] buff = new byte[1024];
int count;
while ((count = in.read(buff)) != -1) {
fos.write(buff, 0, count);
}
}
String firstname = multipart.getFormDataPart("firstname", String.class, null);
String lastname = multipart.getFormDataPart("lastname", String.class, null);
return Response.ok(firstname + ":" + lastname).build();
}
如果你想将所有东西放入POJO,你可以这样做
If you want to put everything into a POJO, you can do something like this
public class MyEntity {
@FormParam("firstname")
private String firstname;
@FormParam("lastname")
private String lastname;
@FormParam("file")
private byte[] file;
// Getter and Setters
}
然后在你的资源方法
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(@MultipartForm MyEntity entity) throws IOException {
try (FileOutputStream fos = new FileOutputStream("file.png")) {
byte[] filebytes = entity.getFile();
fos.write(filebytes);
}
String firstname = entity.getFirstname();
String lastname = entity.getLastname();
return Response.ok(firstname + ":" + lastname).build();
}
查看更多:
- Multipart Providers
这篇关于如何在角度和重复的同一请求中上传图像和数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!