0

I've read and followed an example file uploading via commons-fileupload api. It works when I post a file, but when I include normal fields like input type="text" it seems like values of field name comes as null. In this case field name as email. Here is a code I am using. Hope experienced eyes of developers out there can point out to me what I am doing wrong. Thank you and have a blessed day!

code in servlet class

        try {                           
            List<FileItem> items = upload.parseRequest(request);

            for (FileItem item : items) {
                if (item.isFormField()) {
                    if(item.getFieldName().equals("email")) { 
                        payLoad.append("&email=" +   item.getString());

                }
                else         {                                              
                    encodedBase64 = new String(Base64.encodeBase64(item.get()));                
                    payLoad.append("&content=" + encodedBase64);
                }
            }
            payLoad = payLoad.deleteCharAt(0); // payLoad = new StringBuffer()
4

1 に答える 1

0

このコードを使用してみてください(このコードは私にとってはうまくいきます)

private Map<String, String[]> parameterMap = new HashMap<String, String[]>();
for(FileItem item: items){
   if (!item.isFormField()) {
    ...
   } else{
     String field = item.getFieldName();
     String content = item.getString();
     parameterMap.put(field,new String[]{content});
   }
 }

次に、parameterMapのすべてのコンテンツを印刷できます。フォームフィールドには「email」以外の名前が付いていると思います

于 2013-10-29T18:32:44.033 に答える