Reputation: 51110
I have some code which reads a file through a form field of type file
<input type="file" ... />
I want to give the user another option of providing a url to a file rather than having to upload it as many are already online.
How can I detect when this field is empty on the server side. I am using Apache Commons FileUpload
FileItemStream item = iter.next();
name = item.getFieldName();
stream = item.openStream();
if(!item.isFormField()){
if(item.toString()!=""){
....
I need to detect when item
is empty. The above code doesn't work, nor does using:
if(item.equals(null)){
....
Upvotes: 0
Views: 3193
Reputation: 1
To check for any empty file input in the form while uploding any file to the server best way follow my instructions 1. use @MultipartConfig() at the top of your servlet class 2. add the following method to your class
private InputStream getImageStream(HttpServletRequest request, String image){
try {
Part part = request.getPart(image);
String header = part.getHeader("content-disposition");
InputStream input = null;
if(header.contains("filename")){
input = part.getInputStream();
}
return input;
} catch (IOException | ServletException e ){
e.printStackTrace();
}
return null;
}
Code description
return the InputStream object
And in your get or post method call the above method with the code below
InputStream school_pic = getImageStream(request, "schoolPic");
where "schoolPic" is the name of your input file in the form
That is all gusy
Upvotes: -1
Reputation: 39733
You can't call item.equals( null )
when item is null. You have to test it like this:
if( item == null ) {
...
}
Upvotes: 4