Getting values of checkboxes along with uploading the file

天涯浪子 提交于 2021-02-10 05:47:07

问题


I was trying to upload an image in java using commonsIO and Jar for file upload.But the problem am facing is that i am having two checkboxes on my form as follow :

<div style="float:left">
  <input name="Mcheckbox" type="checkbox" value="Mobile"/>
  Mobile
</div>
<div style="float:right">
  <input name="Echechkbox" type="checkbox" value="Email"/>
  Email
</div>

And then browse option :

Upload Image : <INPUT TYPE="file" NAME="file" value="file"></input>

And in my servlet am doing something like this :

ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
List<FileItem> files = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
Iterator it = files.iterator();

And then:

while (it.hasNext()) {
    FileItem fi = (FileItem) it.next();

    if (fi.isFormField()) {
        if (fieldname.compareTo("Mcheckbox") == 0) {

            System.out.println("checkbox value is " + fieldvalue);
            mymobilepass = fieldvalue;
        } else if (fieldname.compareTo("Echeckbox") == 0) {

            System.out.println("checkbox value is " + fieldvalue);
            myemailpass = fieldvalue;
        }

    } else {
        InputStream is = fi.getInputStream();

        FileOutputStream fos = new FileOutputStream(path + "\\" + fi.getName());
        int x = is.read();

        while (x >= 0) {
            fos.write((byte) x);
            x = is.read();
            //System.out.println("reading");

        }
    }
}

But am getting null as the value of myemailpass or mymobilepass everytime.What can be the reason ? The if statement is never executed .Why ? Please help


回答1:


The problem is that you do not set fieldvalue in your loop

DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> files = new ServletFileUpload(factory).parseRequest(request);

for (FileItem item : files) {
    if (item.isFormField()) {
        String fieldname = item.getFieldName();
        String fieldvalue = item.getString();
        if (fieldname.compareTo("Mcheckbox") == 0) {
            System.out.println("checkbox value is " + fieldvalue);
            mymobilepass = fieldvalue;
        } else if (fieldname.compareTo("Echeckbox") == 0) {
            System.out.println("checkbox value is " + fieldvalue);
            myemailpass = fieldvalue;
        }

    } else {
        InputStream is = item.getInputStream();

        FileOutputStream fos = new FileOutputStream(path + "\\" + fi.getName());
        int x = is.read();

        while (x >= 0) {
            fos.write((byte) x);
            x = is.read();
            //System.out.println("reading");

        }
    }
}



回答2:


Try in this way ,

 DiskFileUpload upload = new DiskFileUpload();
        String Chk1= "";
        String Chk2= "";
        try {

            List<FileItem> files= upload.parseRequest(request);
            for (FileItem item : files) {
                if (item.isFormField()) {
                    if (item.getFieldName("Mcheckbox")) {
                       //do some thing here 
                    } 
                }

Hope this helps!!



来源:https://stackoverflow.com/questions/23101319/getting-values-of-checkboxes-along-with-uploading-the-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!