I have written a Rest Service in Jersey to upload a Multiple Files. As shown Below. But i want the attribute name i.e name="metadata" and name="file " in Restservice class.
Select XML file 1: <input type="file" **name="metadata"** size="45" accept=".xml" />
Select PDF file 2: <input type="file" **name="fileak**" size="45" accept=".pdf" />
Select XML file 1:
Select PDF file 2:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(
@FormDataParam("file") List<FormDataBodyPart> parts) {
for (FormDataBodyPart part : parts) {
FormDataContentDisposition disp = part
.getFormDataContentDisposition();
InputStream in = part.getValueAs(InputStream.class);
}
return Response.ok(" uploaded successfully !!").build();
}
FormDataContentDisposition only pulls the content type , file name from the form and not the input type name="" attribute.
Any help would be highly appreciated.
I am posting an request using HTML as show below.
Posting the HTML file as wel.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta content="utf-8" http-equiv="encoding" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#uploadBtn").click(function()
{
$('input[type="file"]').each(function(index, value)
{
var nameValue=value.attributes[0].value;
var file = value.files[0];
if(file)
{
var formData = new FormData();
formData.append('file', file);
//formData["name"] = nameValue;
$.ajax({
url : '/publicationservice-web/v1/publication/upload',
type : 'POST',
data : formData,
cache : false,
contentType : false,
processData : false,
name:nameValue,
success : function(data, textStatus, jqXHR) {
var message = jqXHR.responseText;
$("#messages").append("<li>" + message + "</li>");
},
error : function(jqXHR, textStatus, errorThrown) {
$("#messages").append("<li style='color: red;'>" + textStatus + "</li>");
}
});
}
});
});
});
</script>
</head>
<body>
<h1>NGBulletin Upload System - Metadata and PDF</h1>
<form action="v1/publication/upload" method="post" enctype="multipart/form-data">
<p>
Select XML file 1: <input type="file" name="metadata" id="metadata" size="45" accept=".xml" />
</p>
<p>
Select PDF file 2: <input type="file" name="fileak" id="fileak" size="45" accept=".pdf" />
</p>
<p>
<input id="uploadBtn" type="button" value="Upload PFD Files" />
</p>
</form>
<ul id="messages">
</ul>
</body>
</html>
The thing is, the value in @FormDataParam("file")
is the name. You use this when you want all the parts extracted by name. For instance you can have
post(@FormDataParam("metadata") InputStream metaIn,
@FormDataParam("metadata") FormDataContentDisposition metaFcd,
@FormDataParam("fileak") InputStream fileakIn,
@FormDataParam("fileak") FormDataContentDisposition fileakFcd) {
}
But if you want to iterate through all the parts yourself, you should use FormDataMultiPart
instead of List<FormDataBodyPart>
. You can get the map, with the name as the key. Also the name is provided in the FormDataBodyPart.getName()
. For example
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadAsset(FormDataMultiPart multipart) {
Map<String, List<FormDataBodyPart>> map = multipart.getFields();
for (Map.Entry<String, List<FormDataBodyPart>> entry : map.entrySet()) {
for (FormDataBodyPart part : entry.getValue()) {
InputStream in = part.getEntityAs(InputStream.class);
String name = part.getName();
System.out.println("--- name: " + name);
}
}
return Response.ok("cool upload").build();
}
The point is that it is pretty pointless to try and obtain the name (programmatically) if you are going to use @FormDataParam
annatotion, because you are ultimately already hard coding the name (in the annotation value), so you already know it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments