I have application where I am using angular. Input a csv file and add the file to form. below is the code used
var fileInput = document.getElementById('chosenFile');
var fd = new FormData();
fd.append('fileMetadata', JSON.stringify(imgMetadata));
var file = fileInput.files[0];
fd.append('file', file);
now I call http post to send file to my server,
enter code here
$http.post(uploadurl, fd, {
transformRequest: angular.identity,
transformResponse: angular.identity,
headers: {
"X-XSRF-TOKEN": PluginHelper.getCsrfToken(),
"Content-Type": undefined
}
}).then(function successCallback(response) {
console.log("In success callback");
var status = response.status;
if(status == "200")
{
var isValid = response.data;
console.log("FIleupload function in java "+isValid);
if(isValid == "filecopied")
{
$scope.errorText = "filecopied :";
}
}
}, function errorCallback(response) {
console.log("In error callback");
$scope.errorText = "Error in callback file function";
});
At my server end I have written java code to read the file.
@POST
@Path("/uploadfile/{csvdata}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@AllowAll
public Response uploadfile(@PathParam("csvdata") String csvdata, @FormDataParam("fileMetadata") String fileMetadata,
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail, FormDataMultiPart multiPartData, @FormDataParam("fd") InputStream formdata) throws Exception {
try {
if (uploadedInputStream == null) {
log.error("Error: uploadedInputStream is null");
//return Response.status(Response.Status.BAD_REQUEST).entity("File input stream is null").build();
}
if (formdata == null) {
log.error("Error: formdata is null");
//return Response.status(Response.Status.BAD_REQUEST).entity("File input stream is null").build();
} else {
log.error("Error: formdata is NOT null");
}
// for file field
final FormDataBodyPart filePart = multiPartData.getField("<file_field_name>");
final ContentDisposition fileDetails = filePart.getContentDisposition();
final InputStream fileInputStream = filePart.getValueAs(InputStream.class);
log.error(filePart + "-" + fileDetails + "--" + fileInputStream);
log.error("TRY FILEUPLOAD: " + csvdata);
log.error("File Metadata: " + fileMetadata);
// Process the uploaded file
String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();
log.error("uploadedFileLocation: " + uploadedFileLocation);
log.error("fileDetail: " + fileDetail.getSize());
saveToFile(uploadedInputStream, uploadedFileLocation);
return Response.status(Response.Status.OK).entity("filecopied").build();
} catch (Exception e) {
log.error("catch uploadfile: " + e.toString() + "-" + e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Cannot copy!!").build();
}
}
private void saveToFile(InputStream uploadedInputStream, String target) {
try (OutputStream out = new FileOutputStream(new File(target))) {
log.error("TRY saveToFile");
int read;
byte[] bytes = new byte[1024];
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
} catch (IOException e) {
log.error("catch saveToFile: " + e.toString() + "-" + e.getMessage());
}
}
if you see parameters of the method,
1- @PathParam("csvdata") String csvdata, @FormDataParam("fileMetadata") String fileMetadata,
2- @FormDataParam("file") InputStream uploadedInputStream,
3- @FormDataParam("file") FormDataContentDisposition fileDetail,FormDataMultiPart
4- multiPartData,@FormDataParam("fd") InputStream formdata
I have tried above options but uploadedInputStream and InputStream comes always null.
i am able to get
csvdata
fileDetail
fileMetadata
but i am unable to get file from xhtml to java API.
Need help in reading file from form .
I have tried passing multiple parameters at java code but no luck.
Sample output i get is
uploadedInputStream is null
formdata is null
catch uploadfile: java.lang.NullPointerException-null