Good afternoon, I hope you can help me. I'm trying to send a form with the xhr object. I have this in my html page
<form action="" method="post" enctype="multipart/form-data">
<p>
Choose a file : <input type="file" name="archivos"/>
</p>
<input type="submit" value="Upload" onclick="sendForm(this.form)"/>
</form>
the action of the form I'm doing with the xhr object, in my javascript file the following
function sendForm(form) {
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open('POST','http://localhost:8080/project/webresources/generic/archivo',false);
xhr.send(formData);
}
the previous url's of my archive rest Rest In my file I have the following:
@POST
@Path("/archivo")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public void uploadFilebyRest(
FormDataMultiPart multiPart)
throws IOException{
List<FormDataBodyPart> fields = multiPart.getFields("archivos");
for (FormDataBodyPart field : fields) {
handleInputStream(field.getValueAs(InputStream.class));
}
}
then with the method handleInputStream I receive the InputStream achievement and create the file on my hard drive
private String handleInputStream(InputStream uploadedInputStream) throws IOException {
byte[] Arraybytes = IOUtils.toByteArray(uploadedInputStream);
InputStream input = new ByteArrayInputStream(Arraybytes);
int data = input.read();
String uploadedFileLocation = "c://Imagenes/Foto.jpg";
OutputStream out;
out = new FileOutputStream(new File(uploadedFileLocation));
while (data != -1) {
out.write(data);
data = input.read();
}
out.flush();
out.close();
input.close();
return "";
}
All the above code works for me using java EE 6 and Glassfish 3 Someone tell me why I could not work when using Java EE 7 and Glassfish 4 I have the following to my log
415 Unsupported Media Type help me! thanks.