I am using following code to get uploaded file.
@POST
@Path("update")
@Consumes(MediaType.WILDCARD)
public boolean updateWorkBookMaster(MultipartFormDataInput input) {
try {
//get form data
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
//get uploaded file
List<InputPart> inputParts = uploadForm.get("workBookFile");
MultivaluedMap<String, String> header = inputParts.get(0).getHeaders();
InputStream inputStream = inputParts.get(0).getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
now i want to check whether this byte[]
is empty or not,
while debugging when i have not uploaded any file it shows its length as 9
, why its 9
not 0
.
There is no such thing as an empty byte (int, long, float, ...). Every byte (int, long, float, ...) has a value inside. Value can be zero, but that's is not empty. Zero is a legal value, just like any other.
To check if an given array is empty or not, we can use the built-in Array. Length property in C#. Alternatively, we can also use the Array. Length property to check if a array is null or empty in C#.
Only object references can be null; bytes can be zero, though, as you obviously know, and that's what you meant. In your code snippet, the loop is completely unnecessary; when you allocate the array, all the elements are guaranteed to be zero already.
In general Java terminology, an empty byte array is a byte array with length zero, and can be created with the Java expression new byte[0] .
You can implement null check for files this way:
import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
@POST
@Path("update")
@Consumes(MediaType.WILDCARD)
public boolean updateWorkBookMaster(FormDataMultiPart multiPartData) {
try {
final FormDataBodyPart workBookFilePart = multiPartData.getField("workBookFile");
final ContentDisposition workBookFileDetails = workBookFilePart.getContentDisposition();
final InputStream workBookFileDocument = workBookFilePart.getValueAs(InputStream.class);
if (workBookFileDetails.getFileName() != null ||
workBookFileDetails.getFileName().trim().length() > 0 ) {
// file is present
} else {
// file is not uploadded
}
} ... // other code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With