我想接收从我的客户端上传的多个文件。我上传了多个文件,并使用JAX-RS
(Jersey)请求服务器端(Java)。
我有以下代码,
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void upload(@Context UriInfo uriInfo,
@FormDataParam("file") final InputStream is,
@FormDataParam("file") final FormDataContentDisposition detail) {
FileOutputStream os = new FileOutputStream("Path/to/save/" + appropriatefileName);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
}
如何在服务器端单独写入在客户端上传的文件
例如。上传了My_File.txt, My_File.PNG, My_File.doc
等文件。我需要在服务器端写入与上述My_File.txt, My_File.PNG, My_File.doc
相同的内容
我怎样才能做到这一点?
你可以尝试这样做:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void upload(FormDataMultiPart formParams)
{
Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields();
// Usually each value in fieldsByName will be a list of length 1.
// Assuming each field in the form is a file, just loop through them.
for (List<FormDataBodyPart> fields : fieldsByName.values())
{
for (FormDataBodyPart field : fields)
{
InputStream is = field.getEntityAs(InputStream.class);
String fileName = field.getName();
// TODO: SAVE FILE HERE
// if you want media type for validation, it's field.getMediaType()
}
}
}
有一个您正在寻找的场景的博客。希望这对你有所帮助http://opensourzesupport.wordpress.com/2012/10/27/multiple-file-upload-along-with-form-data-in-jax-rs/