If uou are invoking the servlet from outside cq, make sure you authenticate in the http request.
Here is an article that posts files to cq from a java swing app
https://helpx.adobe.com/experience-manager/using/post_files.html
This example posts using this java code:
private static void postAEM(File file, String host, String path, String mimeType)
{
try {
String aemPostUrl = host+"/bin/upfile";
HttpPost post = new HttpPost(aemPostUrl);
org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity ();
byte[] b = new byte[(int)file.length()];
org.apache.http.entity.mime.content.FileBody fileBody = new org.apache.http.entity.mime.content.FileBody(file, mimeType) ;
org.apache.http.entity.mime.content.StringBody imageTitle = new org.apache.http.entity.mime.content.StringBody(path);
entity.addPart("imageTitle", imageTitle);
entity.addPart("image", fileBody);
post.setEntity(entity);
org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
org.apache.http.HttpResponse response = null;
response = client.execute(post);
System.out.println("Done") ;
} catch (Exception e) {
e.printStackTrace();
}
}
This is an example of invoking a servlet from outside cq.